Home > Apache > .htaccess Magical Rules

.htaccess Magical Rules

Useful apache rules:

Regular Exp Tips:
http://www.webmasterworld.com/forum92/4332.htm
==========================================================
Redirecting whole website to “https” using .htaccess

RewriteCond %{HTTPS} !on
 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

=====================================================

The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

To Redirect special chars and sql query from query string

RewriteEngine on
Options +FollowSymlinks
RewriteBase /pages/

RewriteCond %{QUERY_STRING}  ^.*union.*$ [NC]
RewriteRule ^.*$ /notfound.php [L]

RewriteCond %{QUERY_STRING} (”|%22).*(>|%3E|<|%3C).* [NC]
RewriteRule ^.*$ /notfound.php [L]

RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC]
RewriteRule ^.*$ /notfound.php [L]

RewriteCond %{QUERY_STRING} (javascript:).*(;).* [NC]
RewriteRule ^.*$ /notfound.php [L]

RewriteCond %{QUERY_STRING} (;|’|”|#|%22|%23).*(union|select|insert|drop|update|md5|benchmark).* [NC]
RewriteRule ^.*$ /notfound.php [L]

Redirect all but 1 IP to different site, using mod_rewrite

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1\.1\.1\.1
RewriteRule .* http://www.htaccesselite.com [R=302,L]

Redirect Everyone but you to alternate page on your server.


RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1\.1\.1\.1
RewriteCond %{REQUEST_URI} !/temporary-offline\.html$
RewriteRule .* /temporary-offline.html [R=302,L]


How to protect files and folder display If there is no index.html:

IndexIgnore * (for all)
IndexIgnore *.pdf *.mp3  (to specific blocking)

=============================================================================
How to Protect any folder using apache (.htaccess and .passwd)

Help URL:

http://www.digitalindigo.net/support/web/htaccess.html

.htaccess

AuthUserFile /var/www/html/developer/.passwd
AuthName Password_Protection
AuthType Basic

<LIMIT GET POST PUT>
require valid-user
</LIMIT>


.passwd:

admin:$1$SgYzDvJ5$vFEP4TnUKWh0BiCCnJ9dK1


Example passwords:

admin:$1$U6SMGe9t$Go8hxYGSYSsS79iB74exM/ (for 123456)
admin:$1$DXP9j7GG$veWB5JSIlZ/qaA.dtFVFR1 (for auth12345)


======================================================================================


SEO 301 Redirect Single File

Redirect 301 /d/file.html /ddd/file.html

Redirect Home to new Domain

Redirect 301 / http://www.htaccesselite.com

Redirect Entire site to single file:

Note: This is a 302 (temporary)redirect because its meant to point to a temporarily offline file.

RedirectMatch 302 ^/ /temporary-offline.html


Changed file extension?

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

Redirect non-www to www

Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

Redirect www to non www version of site

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


Redirect example.com/index.php to example.com/

# index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3, 9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]


Method 2 - Meta Redirect
<meta http-equiv="refresh" content="10; url=http://example.com/">

# display all php page as .html in URL

RewriteRule ^(.*)\.htm$ 1.php

RewriteRule ^(.*)\.(js|ico|gif|jpg|png|css)$ index.php

.htaccess - mod_rewrite for changed url’s

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

The old URL of a message in the forum looks like this:
http://www.skitx.com/forum/index.php?topic=8283.msg90999

This should be redirected to:
http://www.skitx.com/forum/viewthread/8283/


Setting HTTP/HTTPS Environment Variable

Old method for HTTP <-> HTTPS Redirection

This is the old way I would have to use to redirect /index.html to / and urls with // to /.


RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.html\ HTTP/ [NC]
RewriteRule ^.*$ http://%{SERVER_NAME}/%1 [R=301,L]  
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.html\ HTTP/ [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}/%1 [R=301,L]  
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*)\ HTTP/ [NC]
RewriteRule ^.*$ http://%{SERVER_NAME}/%1/%2 [R=301,L]  
RewriteCond %{HTTPS} =on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*)\ HTTP/ [NC]
RewriteRule ^.*$ https://%{SERVER_NAME}/%1/%2 [R=301,L]

New HTTPS <-> HTTP Redirection

First I set the environment variable ps to have the value “http” for HTTP requests, or “https” for HTTPS requests. Once that is accomplished, I can use %{ENV:ps} in all of my rewriterules and it will result in https for SSL requests and http for non-ssl requests!


RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+)$ - [env=ps:http]  

# redirect urls with index.html to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.html\ HTTP/ [NC]
RewriteRule ^.*$ %{ENV:ps}://%{SERVER_NAME}/%1 [R=301,L]  

# change // to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*)\ HTTP/ [NC]
RewriteRule ^.*$ %{ENV:ps}://%{SERVER_NAME}/%1/%2 [R=301,L]

Even Newer HTTP/HTTPS Rewrite Code

The top guru I have ever seen in my lengthy .htaccess related web travels is a moderator on the WebmasterWorld.com Apache Forum, jdMorgan. Upon seeing the above solution that I came up with, jdMorgan instantly provided an improvement, resulting in being able to set the environment variable in 1 rewrite block instead of 2. This is truly Sweet.


RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.+)$ - [env=askapache:%2]  

# redirect urls with index.html to folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http%{ENV:askapache}://%{HTTP_HOST}/$1 [R=301,L]  

# change // to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)//(.*)\ HTTP/ [NC]
RewriteRule ^.*$ http%{ENV:askapache}://%{HTTP_HOST}/%1/%2 [R=301,L]


1)Rewriting product.php?id=12 to product-12.html

It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.

RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

2) Rewriting product.php?id=12 to product/ipod-nano/12.html

SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.

RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2

3) Redirecting non www URL to www URL

If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.

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

4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz

Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

5) Redirecting the domain to a new subfolder of inside public_html.

Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

Author: garima Categories: Apache Tags:
  1. No comments yet.
  1. No trackbacks yet.