Frequently Asked Questions

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

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

Removing (.php) file extensions.

To remove .php (for example) file extensions put the following in a .htaccess file in your document root

No trailing slash:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1 [R=301,L]

# Rewrite to add .php back.
RewriteCond %{REQUEST_URI} ^(/.+)$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*[^/])$ %1.php [QSA,L]


With a trailing slash:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(/.+)\.php$
RewriteRule ^(.+)\.php$ %1/ [R=301,L]

# Rewrite to add .php back.
RewriteCond %{REQUEST_URI} ^(/.+)/$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^.+/$ %1.php [QSA,L]


To remove a different file extension replace all references to php with the file extension you want to remove.
Last edited by richardk on Mon Sep 28, 2009 2:16 pm, edited 5 times in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Sat Jun 06, 2009 4:05 pm

Removing index files.

To remove all index.html files from the URL
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.html$ /%1 [NC,R=301,L]


To remove all index.html, index.htm and index.php files from the URL
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.(html?|php)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.(html?|php)$ /%1 [NC,R=301,L]


To remove all index.* files from the URL
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.([^/]+)(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.([^/]+)$ /%1 [NC,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Sat Jun 06, 2009 4:23 pm

Hotlinking protection (using HTTP_REFERER).

To deny access to files based on the previous page a user visited or the page in which the file (eg. an image) is being displayed you can use the HTTP Referer header. The Referer header is unreliable, though. It can be changed by users, web crawlers/bots and firewalls (to protect user privacy) or not sent at all (also to protect user privacy).

To allow access to certain extensions (.jpg and .png) only from example.com and www.example.com
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_REFERER} ^(http://(www\.)?example\.com(/.*)?)?$ [NC]
RewriteRule .\.(jpg|png)$ - [F,L]

You can chnage/add to/remove extensions here
Code: Select all
(jpg|png)

for exmaple
Code: Select all
(jpg|bmp|mp3)


To allow access to a certain directory (/dir) only from example.com and www.example.com
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_REFERER} ^(http://(www\.)?example\.com(/.*)?)?$ [NC]
RewriteRule ^dir(/.*)?$ - [F,L]


Instead of sending the HTTP Forbidden (304) status (like in the above examples) you could redirect to a file by replacing
Code: Select all
- [F,L]

with
Code: Select all
/file.ext [R,L]


You can allow more domains by adding
Code: Select all
RewriteCond %{HTTP_REFERER} ^(http://(www\.)?anotherexample\.com(/.*)?)?$ [NC,OR]

before
Code: Select all
RewriteCond %{HTTP_REFERER} ^(http://(www\.)?example\.com(/.*)?)?$ [NC]


You can allow access from a more specific path, eg. example.net/directory, by using
Code: Select all
^(http://(www\.)?example\.com/directory(/.*)?)?$

instead of
Code: Select all
^(http://(www\.)?example\.com(/.*)?)?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby richardk » Tue Jun 23, 2009 11:59 am

Testing many websites on your development computer/server.

If you want to test many websites on your development computer/server you do not have to use sub directories of localhost or different ports. You can set up local domains and use <VirtualHost>s to get seperate document roots for each.

Creating local domains.

You can create these local domains in two ways: the hosts file or a PAC (proxy auto-config) file. These domains will only exist on computers you configure.

Adding entries to your hosts file
Code: Select all
127.0.0.1 localhost

# Local Domains
127.0.0.1 anewname
127.0.0.1 adifferentname.tld www.adifferentname.tld
127.0.0.1 a.sub.domain.tld


You can create a PAC file and load it into your browser. PAC files use JavaScript and can create wildcards (get your own local TLD or wildcard sub domains) and do other complicated things. An example PAC file
Code: Select all
function FindProxyForURL(url, host)
{
   if(isPlainHostName(host)
     || dnsDomainIs(host, '.internal'))
   {
      return 'PROXY 127.0.0.1:80; DIRECT';
   }
   else
   {
      return 'DIRECT';
   }
}

Any host name without a dot (PlainHostName) or any .internal domain name will be sent to 127.0.0.1:80. Everything else will continue as normal (DIRECT).

For both of these you can use a computer's IP address instead of 127.0.0.1 and use the hosts file or PAC file on many computers.

Configuring Apache to handle the local domains.

Again there are two ways. You can either configure them separately
Code: Select all
<VirtualHost *:80>
   ServerName          anewname

   DocumentRoot        /somewehre/anewname
</VirtualHost>
<VirtualHost *:80>
   ServerName          adifferentname.tld
   ServerAlias         www.adifferentname.tld

   DocumentRoot        /something/totally/different
</VirtualHost>
<VirtualHost *:80>
   ServerName          a.sub.domain.tld

   DocumentRoot        /another/a.sub.domain.tld
</VirtualHost>


Or use mod_vhost_alias to dynamically configure them
Code: Select all
# Catch all.
# http://example/ to /path/example/
# http://example.com/ to /path/example.com/
<VirtualHost *:80>
   ServerName          _default_

   DocumentRoot        /path/
   VirtualDocumentRoot /path/%0
</VirtualHost>

# For .internal
# http://example.internal to /different/example/
<VirtualHost *:80>
   ServerName          a.internal
   ServerAlias         *.internal

   DocumentRoot        /different/
   VirtualDocumentRoot /different/%-2+
</VirtualHost>

# A "normal" document root for example.org.
<VirtualHost *:80>
   ServerName          example.org
   ServerAlias         *.example.org

   DocumentRoot        /another/example.org
</VirtualHost>

Note: A problem with VirtualDocumentRoots is that it does not change the DOCUMENT_ROOT variable.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Previous

Return to Beginner's Corner

Who is online

Users browsing this forum: Majestic-12 [Bot] and 3 guests

cron