views:

24

answers:

1

Hello,

I started finally to understand Apache mod_rewrite. It's pretty GREAT!

Plz have a look at the followings:


1) Permanent redirects "http://www.domain.com/folder_name/" (with or without final slash and with or without the 'www') to "http://www.domain.com/some/path/some_page.html"

RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]

2) Permanent redirects all requests to "www.domain.com..." to same path and file request but without 'www' in domain

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

They all work as expected and do their jobs, I'm simply curios if some guy, who is more expert than me in mod_rewrite, could give me some advises like: "it could do be better in this way...", "there might be a problem if...", etc.

Thanks!

+1  A: 
  1. Use the ? quantifier instead of * and you don’t need to escape the substitution URL:

    RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
    
  2. You might want to consider HTTP 1.0 requests where the Host header field is missing. Another useful extension would be to take HTTPS into account:

    RewriteCond %{HTTP_HOST} !^(|example\.com)$
    RewriteCond %{HTTPS} ^on(s)|
    RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]
    
Gumbo
+1 thanks, very interstimng suggestions!
Marco Demajo