[tips] Apache Mod Rewrite tips and tricks

6:27 AM

(0) Comments

hi there
we will talk to day about the most common techniques on the web nowadays – yes thats right we will talk about mod rewrite that awesome module that brings static urls benefits into dynamic contents
ok lets start

 

1- check for the module before rewrite:-

before you use rewrite check for the module in htaccess by doing this

<IfModule mod_rewrite.c>
#here is the rewrite rules and conditions
</IfModule>

this is more tidy and clean code it just ensure the you have mod rewrite module installed and loaded with apache before run the rewrite code

2- rewrite css and images and other files:-

on of the most issues you will gonna face in rewrite is image and css not being rewrite in the web
here is example

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule (.*)  index.php?url=$1  [L]
</IfModule>

this rule will rewrite all urls to index.php with parameter url but in this conditions all urls will rewrite including images and css and js files and that will produce errors
so we can fix this problem by telling rewrite that rewrite only notfound urls but the existing files will exculde from rewrite by adding this two lines before rewrite rule

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

the first line will exclude exsiting folders and the second will exclude files
here is the last code

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)  index.php?url=$1  [L]
</IfModule>

3- rewrite and wildcard domains:-

do you want to controll subdomains via script this is the right choice
we will do this in steps
first you just will added *.domain to be treated like www.domain.com by adding this line in httpd.conf

ServerAlias www.domain.com domain.com *.domain.com

then you have to create the rewrite

<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) %2/$1 [L]
</IfModule>

4- with www or without it :-

you can use rewrite to force www to be added in the domains , that is very useful is search engine optimization – SEO
you can do it by this code

<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
</IfModule>

5- 301 redirect :-

if you want to redirect page to another never ever use meta redirect or javascript redirect because its too bad for search engines coz its a common way for the spammers and absolutely you dont want to be  a spammer in search engines eye
the right way is 301 redirect ( search engine friendly redirect )
you can do it like that :

<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^moved_page.html$  new_page.html [R=301,L]
</IfModule>

you will notice the we used tag R with value 301 that means we will refresh with 301 notice the means moved permanently
thats all i have for now
if you have any questions dont hesitate comment here :)

eslam

, , ,

0 Responses to "[tips] Apache Mod Rewrite tips and tricks"

Post a Comment