Apache mod_rewrite query string before page and remove extention

Go To StackoverFlow.com

0

I've scavenged the web for answers to my mod_rewrite woes and I feel I'm at the end of my wits. I will have a URL like such: http://www.website.com/dashboard.php?username=stackoverflow. This url isn't the prettiest of such. So my goal is to do a few things here...

  1. Eliminate the extension of the php file (I've been able to do this so far with the code I'll show below, but I don't know that it will stay according to the other things I need to do)
  2. Eliminate the "www" prefix
  3. Move the username query string (only if it's "username", I don't want to match "id" or such) directly after ".com/"
  4. Move the php filename (without the extension) after query string.

The final URL should look like such: http://website.com/stackoverflow/dashboard or perhaps http://website.com/stackoverflow/profile.

The code I have right now which eliminates the file extension is such:

Options +FollowSymLinks
RewriteEngine On
RewriteBase / 

# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]

# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]

Sadly this only fixes one of my issues and after looking at it, I'm getting the feeling there would be a better way to do this...

2012-04-04 04:58
by cereallarceny
I'd suggest you to read the very interesting "optimizing website" rules of the YSlow plugin, and you may probably guess why it's a bad idea to remove "www", unless your static content points to another domain name - Olivier Pons 2012-04-04 06:10
Ahh, good point Olivier... that's a good point. Thank you - cereallarceny 2012-04-04 17:57
Read this very good answer here about www: http://webmasters.stackexchange.com/questions/6107/www-yoursite-com-or-http-yoursite-com-which-one-is-futureproo - Olivier Pons 2012-04-05 07:10


1

(1, 3, 4) As I understand you want to use pretty URLs, but backed by PHP pages. So it should be like this:

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(\w+)/profile$ /dashboard.php?username=$1 [L]

It will make http://website.com/stackoverflow/profile working as http://www.website.com/dashboard.php?username=stackoverflow

(2) # Redirect www.site.com to site.com with 301

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
2012-04-04 05:36
by Eugene Retunsky
What if instead I wanted to have a single page (for instance: home.php) which would then load the contents of dashboard.php or profile.php depending on if statements I write. The URL would look something like http://website.com/stackoverflow. Would that be possible with the rules you've written above - cereallarceny 2012-04-04 17:41
Yes. E.g. RewriteRule ^(\w+)$ /home.php?username=$1 [L] The (\w+) pattern can be different of course. But you need to make sure, that your patterns are not ambiguous. This might be tricky - but you can organize links in way you want using these types of rewrites. I use this approach for my site - Eugene Retunsky 2012-04-04 17:50
Wonderful answer Eugene. Thanks for your help - cereallarceny 2012-04-07 19:37
Ads