Reputation: 23
I'll be honest in saying I have very little experience with .htaccess
as I've always wanted to stay away from it as best I can. However, I've recently wanted to tidy up my urls and I've found that it's possible through .htaccess
and rewriting.
Basically, I want to rewrite a url like:
www.mysite.com/profile.php?id=48194
To something like:
www.mysite.com/profile/48194
Here's the code I have currently:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^profile/(.*)/$ profile.php?id=$1
The line I'm trying to use is on the very bottom, RewriteRule ^profile/(.*)/$ profile.php?id=$1
. The rest is used to remove the page extensions from the urls. I've changed $1
to $2
thinking perhaps it was conflicting with the code above, but nothing changed.
I also removed all the code except for RewriteEngine on
and the last line thinking maybe the codes were conflicting but, again, nothing changed or worked. The rest of the code does work, removing the extensions from urls that is, so I know the rewrite thing is on.
Could someone try to break down and explain what I did wrong and how all this works? As well as providing a working example of the thing I'm trying to accomplish?
Thanks in advance!
Upvotes: 2
Views: 87
Reputation: 786081
Change order of your rules and use MultiViews
option. Option MultiViews
is used by Apache's content negotiation module
that runs before mod_rewrite
and and makes Apache server match extensions of files. So /file
can be in URL but it will serve /file.php
.
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)/?$ profile.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Upvotes: 0