Frequently Asked Questions

New to mod_rewrite? This is a good place to start.

Postby richardk » Wed Feb 21, 2007 3:48 pm

How to check if mod_rewrite is enabled.

Put the following mod_rewrite in a .htaccess file in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule modrewrite http://www.modrewrite.com/? [NC,R,L]

Now go to http://example.com/modrewrite (where example.com is your domain or IP address). If you are redirected to modrewrite.com, mod_rewrite should be working OK.
Last edited by richardk on Sat Jun 06, 2009 3:46 pm, edited 2 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Wed Feb 21, 2007 3:49 pm

Relative paths to images, JavaScript, CSS and other external/linked files are broken.

The relative paths are broken because they are relative to the URL in the browser's address bar. For example, if you had a URL of /index.php?page=abc and a relative URL of images/image-one.jpg, the browser would look for the file at /images/image-one.jpg. However, if you use mod_rewrite to change the URL to /pages/abc.html, the browser will now look for the image at /pages/images/image-one.jpg and it won't find it.

To stop this you have a few options:
  1. Don't use /. If you don't use slashes (eg. /page-abc.html), the path will be at the same depth and relative URLs won't break.
  2. Use absolute paths (everything after the domain name), eg.
    Code: Select all
    <img src="/images/image-one.jpg" alt="An image of a one">

    You would need to edit all the links in your pages.

    If you want to be able to move your script and are using a server-side language, you can use a variable or constant so you will only have to change one line. For example in PHP, you would place a constant definition in you global include/configuration file, like this:
    Code: Select all
    define('MOD_REWRITE_BASE_PATH', '/');

    And use it in when outputting your the links
    Code: Select all
    <a href="<?php echo MOD_REWRITE_BASE_PATH; ?>images/image-one.jpg">foo</a>

    If you move the files, you only have to change the value of MOD_REWRITE_BASE_PATH and all the paths will change.
  3. There is also a possibility of a mod_reite "hack" to get it to work. In this case, it would be
    Code: Select all
    RewriteRule ^.+(/images/.+)$ $1 [L]
  4. Add a <base>, eg.
    Code: Select all
    <base href="http://www.example.com/">


Edit: Added <base>.
Last edited by richardk on Sat Jun 06, 2009 3:47 pm, edited 3 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Sat Feb 24, 2007 12:22 pm

How to enable mod_rewrite.

To enable mod_rewrite you need access to Apache's httpd.conf file.

You will need to uncomment (remove the # from the beginning of) mod_rewrite's LoadModule line. It should be near the beginning of the httpd.conf file.

If you are going to use mod_rewrite in the httpd.conf file, you need to make sure FollowSymLinks is enabled. So include the following line with your mod_rewrite
Code: Select all
Options +FollowSymLinks


If you are going to use mod_rewrite in .htaccess files you will need to find the <Directory> (in your httpd.conf file, they probably already exist) of your document root(s) to enable FollowSymLinks and allow .htaccess files. To do this add the Options line from above and set the AllowOverride line to All (or add it if it's not there).
Code: Select all
<Directory /path/to/your/document/root>
  Options +FollowSymLinks
  AllowOverride All

  # the rest of this directories configuration
</Directory>


Edit: Added "(in your httpd.conf file, they probably already exist)".
Last edited by richardk on Wed Jan 30, 2008 4:05 pm, edited 1 time in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Mon Mar 05, 2007 2:18 pm

How to change a numeric ID into a name/title.

To change a numeric ID, eg. /file.php?id=123, into a name/title, eg. /files/abc.html, you have four options
  1. Use a RewriteMap. This requires access to the httpd.conf file, not just the ability to use .htaccess files. The RewriteMap line must be in the httpd.conf file (or other Included configuration file), it cannot go in a .htaccess file.

    A RewriteMap is (in this case, it can be other things) a simple text file with name/title => numeric ID pairs, eg. (/path/to/map.txt)
    Code: Select all
    abc 123
    def 456
    ghi 789


    Then the mod_rewrite (in the appropriate <Directory> or <VirtualHost> in the httpd.conf file)
    Code: Select all
    Options +FollowSymLinks

    RewriteEngine On
    RewriteMap name2id txt:/path/to/map.txt

    RewriteRule ^/files/([^/]+)\.html$ /file.php?id=${name2id:$1|0} [QSA,L]

  2. Leave the numeric ID in the URL and add the name/title, eg. /files/123-abc.html. Then you just ignore the name/title in the RewriteRule
    Code: Select all
    RewriteRule ^files/([0-9]+)-[^/]+\.html$ /file.php?id=$1 [QSA,L]

  3. Edit the script to use the name/title instead of the numeric ID. Then you will just need a normal RewriteRule
    Code: Select all
    RewriteRule ^files/([^/]+)\.html$ /file.php?name/title=$1 [QSA,L]

  4. Write a script (script.php) that takes the name/title, finds the associated numeric ID then runs the original script (file.php).
    Code: Select all
    RewriteRule ^files/([^/]+)\.html$ /script.php?name/title=$1 [QSA,L]

  5. Or finally, you could sue a seperate rule for each one, if there is a manageable amount.


So if you want to do this, you must decide on the most appropriate. Can you access the httpd.conf file? Do you mind leaving the ID in the URL? Can you write a script to change the name/title into the ID? Can you edit the current script?

Edit 1: Made it clearer that RewriteMaps must go in the httpd.conf file.
Edit 2: Separate rules.
Last edited by richardk on Wed Aug 13, 2008 12:32 pm, edited 2 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Tue May 29, 2007 12:12 pm

Forcing www.
  • To redirect only example.com to www.example.com, put the following in a .htaccess in your document root
    Code: Select all
    Options +FollowSymLinks

    RewriteEngine On

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

  • To redirect all sub domains and domains that are not www.example.com to www.example.com, put the following in a .htaccess in your document root
    Code: Select all
    Options +FollowSymLinks

    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,L]
Last edited by richardk on Sat Jun 06, 2009 3:44 pm, edited 5 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Tue May 29, 2007 12:13 pm

Removing www.
  • To redirect only www.example.com to example.com, put the following in a .htaccess in your document root
    Code: Select all
    Options +FollowSymLinks

    RewriteEngine On

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

  • To remove www. from all sub domains and domains, put the following in a .htaccess in your document root
    Code: Select all
    Options +FollowSymLinks

    RewriteEngine On

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L]
Last edited by richardk on Sat Jun 06, 2009 3:43 pm, edited 3 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Sun Oct 14, 2007 11:00 am

Redirect all your domains/sub domains to one of your domains.

Note: this will only work if the domains and sub domains go to one document root and sub domain names will be lost.

In a .htaccess file in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com%{REQUEST_URI} [R=301,L]
Last edited by richardk on Sat Jun 06, 2009 3:42 pm, edited 3 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Sun Oct 14, 2007 11:08 am

Virtual sub domains.

Note: this will only work if you have DNS for the sub domains (probably Wildcard DNS) and the server configured to send the requests to the same document root.

For example, abc.example.com and www.abc.example.com to example.com/abc/, def.example.com and www.def.example.com to example.com/def/ etc.

Note: If you have access to the httpd.conf file do not use mod_rewrite, use mod_vhost_alias.

In a .htaccess file in the document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /%2/$1 [QSA,L]


You can rewrite to a file based on the sub domain name. For example, abc.example.com/* and www.abc.example.com/* to example.com/dir/abc.ext?request_uri=*, def.example.com/* and www.def.example.com/* to example.com/dir/def.ext?request_uri=* etc. In a .htaccess file in the document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /dir/%2.ext?request_uri=$1 [QSA,L]


You can rewrite to a file and add query string parameters based on the sub domain name. For example, abc.example.com/* and www.abc.example.com/* to example.com/file.ext?sub_domain=abc&request_uri=*, def.example.com/* and www.def.example.com/* to example.com/dir/file.ext?sub_domain=def&request_uri=* etc. In a .htaccess file in the document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Rewrite sub domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ /file.ext?sub_domain=%2&request_uri=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Add-on domains.

Postby richardk » Sat Jun 06, 2009 3:55 pm

Add-on domain to sub directory.

For an add-on domain to go to a sub directory of your document root try the following in a .htaccess file in your document root
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Fix missing trailing slashes.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/example%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

# Rewrite add-on domains.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ /example/$1 [QSA,L]

Where example.com is the add-on domain.
Last edited by richardk on Tue Sep 08, 2009 7:49 am, edited 3 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 1 guest