Rewrite Rules in Dispatcher
Rewrite Rules on Apache webserver using the module mod_rewrite.
- an Apache module for manipulating(rewriting) URLs.
- meaning, taking the requested URL from visitor and responding them with a different URL.
e.g. visitor hits www.example.com/page.html, the mod_rewrite internally converts it to www.example.com/another_page.html and returns the HTML content back. However, in this process, the prowser URL stays the same as the visitor hit.
- Rewriting and redirecting are 2 different things. Above example was of rewriting.
What can mod_rewrite do?
- lets us create rules to handle the URL manipulation and make cosmetic changes on Apache server/Dispatcher.
e.g. insert values or rearrange strings or remove characters etc.
- let us check server variables e.g. user agent.
- beautify the ugly URLs
e.g. www.ex.com/hello/world to www.ex.com?x=hello&y=world
- stop other site to make use of the resources from our site. respond them with forbidden.
- redirecting to the canonical URLs
e.g. ex.com/hello/world to www.ex.com/hello/world
- avoiding the 404 errors on the page hits from visitor, in cases where we dont wish to change the URL of revamped site but return 200OK to visitor.
How to use the mod_rewrite on Apache server?
- enable the rewrite module in httpd.conf file using/adding below code
LoadModule rewrite_module modules/mod_rewrite.so
- add the below directive to httpd.conf file
<IfModule mod_rewrite.c>
<VirtualHost *:80>
ServerName localhost
ServerAlias 127.0.0.1
LimitRequestFieldSize 32768
AllowEncodedSlashes NoDecode
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/bin
RewriteCond %{REQUEST_URI} !^/content
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/saml_login
RewriteCond %{REQUEST_URI} !^/system
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteCond %{REQUEST_URI} (.html|.jpe?g|.png|.svg|.gif|.GIF)$
</VirtualHost>
</IfModule>
How Rewrite rules works?
- on enabling the mod_rewrite module, we can make use of the RewriteRule directive to create a rewrite rule.
The general syntax of the directive is :
RewriteRule Pattern Substitution [optional flags]
Pattern : a regular expression if match with the incoming URL, respective rule is processed.
Substitution : this is the new processed URL to use instead of the incoming matcher one.
[Optional Flags] : one or more flags to alter the behaviour of the rule.
Practical Uses of Rewrite Rules
mod_rewrite is a powerful Apache module that provides a way to rewrite URLs on-the-fly. Here are some practical and real-world examples of how mod_rewrite can be used:
Redirecting URLs: Redirect users from an old URL structure to a new one.
RewriteEngine On
RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
Removing File Extensions: Serve URLs without the .html or .php extension.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Force HTTPS: Redirect all HTTP traffic to HTTPS.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
User-Friendly URLs: Convert dynamic URLs to more readable and SEO-friendly URLs.
RewriteEngine On
RewriteRule ^products/([a-zA-Z0-9_-]+)$ product.php?name=$1
URL Parameters: Modify or reorder URL parameters.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^product\.php$ /product/%1? [R=301,L]
Blocking Access: Deny access to specific files or directories.
RewriteEngine On
RewriteRule ^private-directory/.*$ - [F]
Custom Error Pages: Redirect users to a custom error page for 404 errors.
ErrorDocument 404 /404.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.html [L]
Mobile Redirection: Redirect mobile users to a mobile-friendly version of the site.
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ https://m.example.com/$1 [L,R=302]
No comments:
Post a Comment