Tag Archives: htaccess

url rewriting guide

Introduction to mod_rewrite

The Apache module mod_rewrite is a killer one, i.e. it is a really sophisticated module which provides a powerful way to do URL manipulations. With it you can do nearly all types of URL manipulations you ever dreamed about. The price you have to pay is to accept complexity, because mod_rewrite’s major drawback is that it is not easy to understand and use for the beginner. And even Apache experts sometimes discover new aspects where mod_rewrite can help.

In other words: With mod_rewrite you either shoot yourself in the foot the first time and never use it again or love it for the rest of your life because of its power. This paper tries to give you a few initial success events to avoid the first case by presenting already invented solutions to you.

Practical Solutions





Here come a lot of practical solutions I’ve either invented myself or collected from other peoples solutions in the past. Feel free to learn the black magic of URL rewriting from these examples. ATTENTION: Depending on your server-configuration it can be necessary to slightly change the examples for your situation, e.g. adding the [PT] flag when additionally using mod_alias and mod_userdir, etc. Or rewriting a ruleset to fit in .htaccess context instead of per-server context. Always try to understand what a particular ruleset really does before you use it in order to avoid problems.

Canonical Hostnames

Description:
The goal of this rule is to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.
Solution:
# For sites running on a port other than 80
RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R]

# And for a site running on port 80
RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://fully.qualified.domain.name/$1 [L,R]

Move Homedirs to Different Webserver

Description:
A lot of webmaster aksed for a solution to the following situation: They wanted to redirect just all homedirs on a webserver to another webserver. They usually need such things when establishing a newer webserver which will replace the old one over time.
Solution:
The solution is trivial with mod_rewrite. On the old webserver we just redirect all /~user/anypath URLs to http://newserver/~user/anypath.

RewriteEngine on
RewriteRule   ^/~(.+)  http://newserver/~$1  [R,L]

source: http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

mod_rewrite cheat sheet





error using mod_rewrite

I got an error using mod_rewrite on my local server and it was only solved when I put  Options +FollowSymLinks but on live server using Options +FollowSymLinks gave me an error so I removed it. Well anyway, I came across this post and it has a nice explanation regarding that.

For reference, if anybody else wants to know:
mod_rewrite can be used to simulate a symbolic link. This is why mod_rewrite requires FollowSymLinks to be enabled, because it’s a similar security thing. So if your host doesn’t enable FollowSymLinks, and you try using mod_rewrite, you’ll get the 403 error. Adding Options +FollowSymLinks will override it for your directory, allowing mod_rewrite to work.

But, if you have this problem and you add Options +FollowSymLinks and then get a 500 error instead, then your host is disallowing the use of the Options directive in .htaccess files, and there’s little you can do but complain to them.

source : http://wordpress.org/support/topic/138301#post-630546

How to make subfolder the main folder for your main domain

I’m organizing our server and wanted to make subfolder the main folder for our main domain. Here’s some code to do it from hostmonster.com

# Hostmonster.com
# .htaccess main domain to subfolder redirect
# Copy and paste the following code into the .htaccess file
# in the public_html folder of your hosting account
# make the changes to the file according to the instructions.

# Do not change this line.

RewriteEngine on

# Change yourdomain.com to be your main domain.

RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$

# Change ‘subfolder’ to be the folder you will use for your main domain.

RewriteCond %{REQUEST_URI} !^/subfolder/

# Don’t change this line.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Change ‘subfolder’ to be the folder you will use for your main domain.

RewriteRule ^(.*)$ /subfolder/$1

# Change yourdomain.com to be your main domain again.
# Change ‘subfolder’ to be the folder you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.

RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

source: http://helpdesk.hostmonster.com/kb/index.php?x=&mod_id=2&id=308