Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite a dynamic url after extension removed

I've been working on my HTAccess for a couple days now, and I've hit a dead end.

I've rewritten and redirected the files to be extensionless, now I need to rewrite the url to be SEO Friendly.

Previously, the URL was: http://www.example.com/member.php?playername=encodedName
I removed the extension: http://www.example.com/member?playername=encodedName
I can't quite get it to: http://www.example.com/member/encodedName

Here's what I've gotten so far:

Code trying to redirect to SEO Friendly URL. Does NOT work.

 RewriteRule ^member/([^/]*)$ /member.php?playername=$1 [L] 
 RewriteRule ^squad/([^/]*)$ /squad.php?squadname=$1 [L] 
 RewriteRule ^article/([^/]*)$ /article.php?articlename=$1 [L]

Unless directory, remove trailing slash. Works.

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)/$ /$1 [R=301,L]

Resolve .php file for extensionless php urls. Works.

 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^(.*)$ $1.php [L]

Redirect external .php requests to extensionless url. Works.

 RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
 RewriteRule ^(([^/]+/)*[^.]+)\.php $1 [R=301,L]

Does anyone have an idea as to what I am doing wrong? It's probably a very newbie problem that I just haven't encountered yet.

like image 686
TbWill4321 Avatar asked Nov 30 '25 12:11

TbWill4321


1 Answers

The "Code trying to redirect to SEO Friendly URL" part looks fine. And while you're redirecting requests made directly to php files to remove the extension, you're not doing anything specifically about the 3 SEO friendly URLs that you are rewriting back.

If when you go to http://www.example.com/member/encodedName, and you're not being served the content at http://www.example.com/member.php?playername=encodedName, then this looks like a Multiviews problem. You'll need to turn off Multiviews either in your server/vhost config or add this to the top of the htaccess file (or in the appropriate already existing Options statement):

Options -Multiviews

In order to do the redirecting to the SEO friendly URLs, add this before the "Redirect external .php requests to extensionless url." rules:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /member\.php\?playername=([^&\ ]+)([^\ ]*)
RewriteRule ^ /member/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /squad\.php\?squadname=([^&\ ]+)([^\ ]*)
RewriteRule ^ /squad/%1?%2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /artcile\.php\?articlename=([^&\ ]+)([^\ ]*)
RewriteRule ^ /article/%1?%2 [L,R=301]
like image 199
Jon Lin Avatar answered Dec 03 '25 14:12

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!