mod_rewrite

Caporegime
Joined
25 Jul 2003
Posts
40,464
Location
FR+UK
I've been playing about with this for a while now, and I'm getting nowhere nearer, and rather then continue to bug my usual source of help I thought I'd ask here.

I'm basically trying to 'clean' my urls, I've read the apache info on mod_rewrite, and tried to find some decent tutorials but there don't seem to be many real beginner friendly guides around.

Eg: domain/index.html to domain/home/, domain/about.html to domain/about/.
 
Code:
RewriteEngine on
RewriteRule ^/index.html$ http://%{HTTP_HOST}/home/ [R=permanent]
RewriteRule ^/about.html$ http://%{HTTP_HOST}/about/ [R=permanent]
That should do it.
 
Sorry for the thread hijack but its pretty much on topic...

I have a number of parked domains. Is this a good way to redirect them all my mydomain?

Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]

Or would it be a better idea to add all the parked domains individually?
Thanks
 
Sorry for the thread hijack but its pretty much on topic...

I have a number of parked domains. Is this a good way to redirect them all my mydomain?

Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.maindomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com/$1 [R=301]

Or would it be a better idea to add all the parked domains individually?
Thanks

I would have thought the best way to do it would be as follows, because it will redirect with or without the 'www.' and it will also redirect anyone using 'maindomain.com' to 'www.maindomain.com'.

Code:
RewriteEngine on

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

RewriteCond %{HTTP_HOST} ^parkeddomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.parkeddomain.co.uk$
RewriteRule ^/?(.*)$ "http\:\/\/www\.maindomain\.com\/$1" [R=301,L]
 
Code:
RewriteEngine on
RewriteRule ^/index.html$ http://%{HTTP_HOST}/home/ [R=permanent]
RewriteRule ^/about.html$ http://%{HTTP_HOST}/about/ [R=permanent]
That should do it.
Thanks for that Whitman, is there a rule I can use to "catch all" or should I write a rule for each link?

Doesn't work btw...I get
Not Found
The requested URL /about/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.​
 
Last edited:
Apologies for the hijack. I've been meaning to have a go of this for ages now. Can see how its done in Apache, but where would I start for hosting in IIS?

Cheers
 
Thanks for that Whitman, is there a rule I can use to "catch all" or should I write a rule for each link?

Doesn't work btw...I get
Not Found
The requested URL /about/ was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.​

My mistake, was doing the wrong thing, try this instead.
Code:
RewriteRule ^/home/$ index.html [L]
RewriteRule ^/(.*)/$ $1.html [L]

That should (not tested) set any request domain.com/foo/ to foo.html with a special case for /home/ to index.html.
 
Apologies for the hijack. I've been meaning to have a go of this for ages now. Can see how its done in Apache, but where would I start for hosting in IIS?

Cheers
ISAPI Rewrite for IIS I believe

Dont quote me on that though, never used that implementation.
 
I used ISAPI Rewrite (http://www.isapirewrite.com/) at my last place, never had a problem with it and the documentation is really good (syntax is very similar to mod_rewrite). This was Server 2003 (IIS6?). The "lite" version if they still do it was free and had good enough for development or simple production.
 
Doesn't work btw..
I think the example given will permanent (301) redirect any requests to /index.html to /home/ (e.g. anyone with an old link will be redirected and have the new tidy URL in their location bar)... which I'm guessing doesn't exist.

I think you need something like:
Code:
RewriteRule ^/home/$ /home.html [L]
RewriteRule ^/about/$ /about.html [L]
To tell the sever that beind the scenes any request to /home/ should actually load /home.html. But I haven't done this for a while, even longer since I did it on Apache. The [L] tells Apache not to process any more rules once this one has matched (stops recursion I think, so put these two before your other two).

It should totally be possible to write a generic version, is the logic /anything/ -> /anything.html? I'd usually have another directory e.g. /content/anything/ so I can still have normal files in the root.
 
Last edited:
Back
Top Bottom