5 awesome htaccess copy and paste Rewrite rules with explanations

I often see people struggling with their mod-rewrite code and when they think they can't fix the issue themselves, they either go to stackoverflow or keep scrolling the search results and hopefully ,perhaps get the solution there.

URL rewriting is not as complicated as new users find it. Like other languages, URL rewriting is a mazic of syntex. If you are learning from the scratch then learning its basic syntex first can help you better understand it and speed up your learning skill.

There are alot of learning resources on the Internet including the official mod-rewrite guide where you can start.


In this post, I am going to share 5 apache RewriteRule scripts that you can easily copy and paste to your server.config or htaccess
file to use them.




#1) Rewriting a long url to make it short and search engine friendly




With apache mod-rewrite, you can turn a longer looking URL into short one.
Let's assume you have long URL with query variables which looks something like the following:
http://example.com/users.php?user=foo

The one main problem with the URL above is that it is longer, not memorable and non-SEF . Search engines often ignore such URLs due to the duplicate URL issues.

To make the above URL short and SEF , you can use RewriteRule in your server.config or htaccess file.
With a RewriteRule you can turn the URL into the following format:

http://example.com/users/foo

The URL looks awasome now. Doesn't it?



RewriteRule script :


You can simply copy and paste the rule above to your htaccess or server.config file and put it bellow "RewriteEngine On" .





#2 Setting an environment variable using RewriteRule



You can set a URI based environment variable on your server using RewriteRule directive. Settings the variable via RewriteRule is easier then setting it in php. You can set an environment variable for specific URIs when these URIs pass through the RewriteRule.
The following RewriteRule code example sets an environment variable named foo and value bar for file.php meaning that when you request the /file.php the rule will automatically set the variable. You can copy the code from textarea bellow and put it in your htaccess file.





RewriteRule script :






#3) Access files without extensions



A simple one liner code to ignore file extensions on Apache server. If you want to access your files without typing extensions at the end, for example to
access file.html as file you can use the following line in your htaccess file :









#4) non-www to www URL redirection



RewriteRules can be used to redirect a non-www (naked domain) to its www version. To redirect your non-www URLs to www , you can use the rule bellow:
RewriteEngine on
RewriteCond %{HTTP_HOST} !www\. [NC]
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,L]

We use RewriteCond (condition) directive to test the host header string. If the test string %{HTTP_HOST} matches the pattern !^www then the rule is applied.. In simple words, if you type yourdomain.com into your browser the rule will redirect it to www.example.com .




#5) Custom 404 rule



With mod rewrite you can create a custom 404 rule for your website. You can redirect your visitors to a specific page on your site when they request a broken or not-found link.

The following RewriteRule script rewrites 404 requests to /file.php on your server. If you are using wordpress, add this before the WordPress default rule .


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.example.com%{REQUEST_URI} [R=301,L]


Post a Comment

0 Comments