One of the most common Webmaster task is to use mod_rewrite Apache module. It’s a flexible and efficient way to redirect URLs. It is useful to redirect non-functional URLs, moving domain names or renaming directories.
Below is a list of some of the frequently used mod_rewrites.
Note the [R=301] entries for 301 Permanent Redirect directive on the rules. It’s a popular use to preserve SEO rankings of an older site that has been moved to a new one.
Simple redirect:
RewriteRule ^/sub/dir/home.html$ /sub/dir2/page.html [R=301,L]
Redirect http://domain.com to http://www.domain.com. This is especially useful for an SSL certificate that’s already registered to www.domain.com name. Note, the rule captures the query string and redirects with it:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
To capture more than one variables in the query string, use the following.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /sub/program.jsp?arg1=$1&arg2=$2&arg3=$3 [L]
For redirects based on the URL’s query string, use QUERY_STRING to capture it for comparison. Note the destination URL may use spaces if enclosed in quotes.
RewriteCond %{QUERY_STRING} ^id=2234$
RewriteRule ^/sub/dir/product.html$ “/sub/dir3/description.html?prodid=vac pro” [L,R=301]
Redirects can also be conditional. For example, redirect everything except with a certain keyword.
RewriteCond %{REQUEST_URI} !/sub/dir/important.html$
RewriteRule ^/sub/dir/.*$ /main/dir/home.html [L,R=301]
With the above rule, it’s possible the original URL may have a query string. To get rid of it, just add “?” to the end of target RewriteRule. For example:
RewriteCond %{REQUEST_URI} !/sub/dir/important.html$
RewriteRule ^/sub/dir/.*$ /main/dir/home.html? [L,R=301]
There are more examples out there. Writing a comprehensive mod_rewrite guide is a full time job, so this list will continue to grow. Here are some other useful references:
- Apache HTTPD mod_rewrite Practical Solutions
- The Art of Web – System: mod_rewrite examples
- A beginner’s guide to mod_rewrite (with examples)
- Use mod rewrite for SEO purposes
Photo Credit: Luke Seeley
Anyone Mod Rewrite Expert care to tackle this one?
I want to have http://example.com/thisTopic rewrite to
http://example.com/t/h/i/thisTopic.html
where the folders t, h, and i are the first 3 letters of thisTopic (and will vary depending on the topic).
Is this possible, and what would the Apache ModRewrite statement look like?
Please, oh Mod Rewrite Gods and Goddesses, help!
Would something like this work?
RewriteRule ^([a-z0-9]+)([a-z0-9]+)([a-z0-9]+)Topic*$ http://example.com/$1/$2/$3/Topic.html [L,R,NC]
i am using jBoss as application server,
i want to do url rewriting, as
url like…
http://www.test.com/product.do?title=digitaltv
rewrite to…
http://www.test.com/product/digitaltv
Please help, i am new to jboss server!
I have the example in the post above. So in your case, it’ll look something like this:
RewriteCond %{QUERY_STRING} ^title=digitaltv$
RewriteRule ^/product.do$ /product/digitaltv [L,R=301]
Hi ,
i need to rewrite the url by appending the country name and language to it. I m using struts 2 framework , opencms and jboss server. If the request url is “\.home.action” then after the struts action mapping, the response url must be like “\US\EN\home.action” . Is it possible to achieve it through jboss rewrite ?.
hi,
i wanna rewrite url on jboss 4.2.3 from http://myserver/openacs/acs to https://myserver/web/tr069
can anyone help me pls?
raghu,
I would try:
RewriteRule ^/\.(.*)$ /US/EN/$1 [L]
cimi,
I would try in your httpd.conf (not the httpd-ssl.conf):
RewriteRule ^/openacs/acs$ https://myserver/web/tr069 [L]