views:

250

answers:

2

This is what I'm trying to do with the htaccess:

  • Force remove the www from the url
  • Be able to use http://website.com/site/ to get to http://website.com/site.php, and force the last slash even if it's not added by the user. So if i write http://website.com/site it will be converted to http://website.com/site/
  • Also translate http://website.com/download.php?=file.exe to http://website.com/download/file.exe. Also http://website.com/news.php?article=43532 into http://website.com/news/article/43532

The htaccess code should not have to contain the domain it's going to be used on.

I've been trying to find something like this on the net for ages, but I only find each part and they don't work together.

Can anyone help me with this?

+2  A: 

Just written these off the top of my head, they might have errors ... but think the jist is right. Comment if there are errors and I'll have a look.

For the www bit:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

The site bit:

RewriteRule ^([^/]*)$ /$1/ [R=301,L]
RewriteRule ^([^/]*/)$ /$1.php

Download:

RewriteRule ^download/(.+)$ /download.php?file=$1

News

RewriteRule ^news/article/([0-9]+)$ /news.php?article=$1
benlumley
Based on the OP's recent comment, all of your rules need 301's, correct?
David Citron
Yes 301. "The site bit" doesn't work when i go to for example website.com, it redirects to website.com//
mofle
@mofle -- Check my answer elsewhere on this page. I fiddled with the site regex a little and I think I fixed it...maybe.
David Citron
@benlumley -- Sorry! I was absolutely not attempting to steal any thunder or reputation from you. The comment format was simply not sufficient for typing regex corrections.
David Citron
+2  A: 

Ok, here's my try. Note that the Apache manual is quite good. I differ from benlumley on the "site" portion. Also, you may want to replace ([0-9]+) with simply (.+) if your news article names are not all numeric.

# Ensure that rewriting is enabled
RewriteEngine on

# First strip the www as benlumley did
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Then convert /site to /site/, reflecting the change in the address bar
RewriteRule ^([^/.]+)$ /$1/ [R=301,L]
# Finally map /site/ to /site.php
RewriteRule ^([^/.]+)/$ /$1.php

# Finally, fix download and news as benlumley did
RewriteRule ^download/(.+)$ /download.php?file=$1
RewriteRule ^news/article/([0-9]+)$ /news.php?article=$1

EDIT: Fixed copy/paste error for the "strip www" part.

NOTE: Any <link href="..."> or <script src="..."> tags that you have will now need to be specified as absolute paths because you have effectively changed the directory that the page appears to be served from.

E.g. consider the following line from your blog.php:

<link rel="stylesheet" type="text/css" href="style.css">

Previously the browser saw http://site/blog.php and thus tried to retrieve http://site/style.css, which worked.

Now that the browser instead sees http://site/blog/ the relative URL above is interpreted as http://site/blog/style.css, which does not exist. As such, the href needs to be changed as:

<link rel="stylesheet" type="text/css" href="/style.css">
David Citron
"RewriteRule ^([^/]*)$ /$1/ [R=301,L]" didn't work, i get the www.website.com// problem. And "RewriteRule ^([^/.]+)$ /$1/ [R=301,L]" and "RewriteRule ^([^/.]+)/$ /$1.php" breaks my site if i visit website.com/site, it adds the slash, but the site is missing it's CSS. Strange. Any thoughts?
mofle
This htaccess file http://pastebin.com/m73d31851 is placed in the root of http://yobird.com, you can try it out yourself, by going to http://yobird.com/blog
mofle
@mofle -- Apologies. I just fixed the script. Please try it again. NOTE! To get your CSS to work now you'll have to use absolute paths for the CSS. Please see above for details.
David Citron
Thanks. It works perfectly ;)
mofle