The three examples on the previous page showed how to rewrite URLs to make sites easier to access and remember.
Rewrite Rules can also have conditions to make sure that the rewrites only take place under specific circumstances.
Example 1: How To Prevent Hotlinking
Hotlinking is the process of using an image or object from one server on another one. This action drains bandwidth from the victim's server and denies the creator of the object any additional visitors to their site that they might have gained otherwise.
You can prevent hotlinking by redirecting all the links to an object on your site to some other less pleasant image, or by forbidding the operation altogether.
RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com/.*$ [NC] RewriteRule .*\.(gif|jpeg|png)$ http://www.example.com/unpleasantness.jpg [R,NC,L]
Now for an explanation:
RewriteRule .*\.(gif|jpeg|png)$ - [F]
Example 2: How to add www to a URL
Another useful trick that mod_rewrite can do is add www to a domain. Although it’s easy for a person to see that example.com and www.example.com are the same site, search engines register them as duplicates, hurting their rankings.
To resolve the issue you can choose to either consistently remove the www or always have it added to the URL. This example will show how to be sure that the www is always attached.
RewriteEngine on RewriteCond %{HTTP_HOST} ^example\.com$ RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
Now for an explanation:
Everything will then convert from example.com to www.example.com
Example 3: Blocking a Specific IP Address
This a useful tool to prevent, for example, malicious parties at specific IP addresses from accessing a site.
RewriteCond %{REMOTE_ADDR} ^(12\.34\.56\.789)$ RewriteRule (.*) - [F,L]
Now for an explanation:
The previous sections have been a basic overview of the capabilities of Mod_Rewrite.
The topic is quite expansive and has many nuances that can make it a very useful and flexible tool.
Here are some links for further information about Mod_Rewrite:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I am missing one “/” here. When I redirect http://www.olddomain.pt/shop/… I get http://www.newdomain.ptshop/…
you can see that between …pt/shop… is missing one “/”
The only way to pass this is put one / in
<VirtualHost *:443> ServerAdmin nunomarques@ioutletstore.pt ServerName http://www.olddomain.pt:443 DocumentRoot /var/www/html/directory Redirect permanent / https://www.newdomain.pt/ …
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^www.newdomain.pt RewriteRule .* https://www.newdomain.pt/%{REQUEST_URI} [R=301,L] </IfModule> Regards
Sweet :]
@tjcrandall: Try adding trailing slashes to the Redirect directives above. Does that fix it?
@tjcrandall: I recommend getting rid of these .htaccess rules and creating a separate virtualhost for mail.example.com and having it redirect traffic to example.com/mail
It should look like this: <pre><VirtualHost *:80> ServerName mail.example.com
Redirect 301 / http://example.com/mail </VirtualHost></pre>
these tutorials are really saving my days here! Thanks, I will continue to be a digital ocean customer just to show support for these tutorials.
I cant seem to get my permalinks working for wordpress, I get an error not found??
Thanks a lot. I got the permalinks working for my Wordpress Installation.