Make your URL human-readable with Mod_rewrite module function
Mod_rewrite module function is to redirect visitor requests which are backed by specific rules which are set. Well, I will be presenting how this treasure tool works in web development. How we can apply Mod_rewrite to our projects.
Why Redirect a URL
- To make URL human-readable
- To avoid the confusion when you see a question mark on a URL
- To step up the level of SEO for our site
- Prevent hotlinking of your images
- Used to block access to sensitive files
Example
http://www.domain.com/updates.php?update=bamenda&political=reginal-meeting-up-station
Change to
http://www.domain.com/bamenda/region-meeting-up-station
Note during website development, your link needs to look like the first URL where you have to pass variables that are visible on the URL. Now you can create a mod_rewrite code to which will be used to redirect the link to the file.
Server Setup
For Mod_rewrite to work probably, we need to make sure our server is configured to run such a script because not all servers enable the mod_rewrite. It's important to check and make sure mod_rewrite is enabled, to be sure to run the PHP code.
phpinfo();
Move to the Apache2Handler section, see if mod_rewrite is listed out. If you can't find it, you have to call your host providers to enable it or quietly move to a different host. Let's look at enabling and testing mod_rewrite on a server.
Get to the Apache Configuration file (httpd.conf file)
change the default configuration by removing #
# LoadModule rewrite_module modules/mod_rewrite.
So your httpd.conf file should look this
<Directory /> Options FollowSymLinks AllowOverride All </Directory>
Restart your apache to save changes.
Now your Mod_rewrite is enabled.
You can check by taking a look at phpinfo(); output.
Let's do a small test to be sure that my mod_rwrite is enabled and it is working well. let me create two files test.html and test.php, Note that the extensions for these files are different
For the Html file
<h2>This is the HTML file.</h2>
For the PHP file
<h2>This is the PHP file.</h2>
Let me create the .htaccess
RewriteEngine on
RewriteRule ^test\.html$ test.php [L]
I'm using localhost, let me make a sample.
http://localhost/mod-test/test.html
It shows that our .htaccess is working well.
Like I mentioned above, it is about specific rules. Let's make more complicated codes now in our .htaccess file. So moving from one URL to another, or manipulating files with different extensions since mod_rewrite can rename all files. What we need is to implement our specifications
- Matching:
You need to look at either matching/redirecting EVERYTHING or NOTHING at all. So to remain on the same depth in a directory structure, it is recommended to eliminate (.*) and for the regex's character set, eliminate /'s. It is good to take off uppercase letters, so you have lowercase, dot, underscore, numbers, and dot.
- Redirection:
It's much advisable to make sure that the redirection can not be matched by another regex, if that happens we have to specify an exclusion. It is to avoid unexpected garbage passed during the redirection loop.
- Mod-rewrite Regex
Regular expressions (regex) knowledge is a core factor when writing mod_rewrite code. I will be going back to the example I made above and will look at and understand what happened.
Example
Change
http://www.domain.com/bamenda/political/region-meeting-up-station
to
http://www.domain.com/updates.php?update=bamenda&political=Reginal-meeting-up-station
Here we need our regex to tell mod_rewrite what to match and display the script. Most developers used (.*) to select NOTHING or EVERYTHING. Note this (.*) are atoms that we can use and separate the subdirectory with slashes. For our case above, the regex will look like this
(.*)/(.*)
#1
(.*): We had two match characters, the dot, and the *. "dot" means ANY character and the "*" specifying Zero or More characters to follow within the atom. So the (.*) will match EVERYTHING in the REQUEST_URL string. REQUEST_URL is the section of the URL that follows after the domain name with you "?". Only with an Apache variable that a RewriteRule can try to match the strings in the URL. For our example, we need to capture the two values in the REQUEST_URL and separate them with slashes.
For the mod_rewrite to know where the URL starts, we used the (^) anchor and ($) anchor to indicate where the URL ends. So our regex will look like this.
^(.*)/(.*)$
#2
so let do the breakdown and what we will finally have. The first atom value is "bamenda" and will be matched as ([a-z]+), which will only permit one or more lowercase letters. Note "+" will metacharacter specifying one or more, while the "*" will metacharacter specifying zero or more of the proceeding characters. Our next atom has an Uppercase letter which is politics, so we used ([a-zA-Z]+). It also advises adding an underscored to take care of the atom if it has an underscored(_) to take care of spaces add \
^ ([a-zA-Z_]+) / ([a-zA-Z_]+)$
#3 to avoid having database problems, try specified every acceptable value with the regex
^(bamenda|political)/([a-zA-Z_]+))$
Sample example
updates.php?update=bamenda&political=Reginal-meeting-up-station
updates.php?update=$1&political=$2
RewriteEngine on
RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)?$
updates.php?update=$1&political=$2 [L]
You can only have "$1" to "$9" as the target string to be used by the mod_rewrite code.
The first atom is still the update.
The second atom is /([a-z]+)?, which is made optional with the trailing?
the third atom is now the state
The RewriteCond(ition) Statement
All that we are doing is learning how to match mod_rewrite with the REQUEST_URL string. Now let's look at setting conditions to access other variables. Both RewriteCond and Rewriterule are similar in that both have the command name, RewriteCond, a variable to match, a regex, and Flags. A common example is trying to add www to your URL. It is important to check if the www. is already there, if not then you can redirect.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]
List Apache Flags
Last|L
nocase|NC
redirect|R
qsappend|QSA
forbidden|F
or|OR
next|N
skip|S=n
mod_rewrite Comments
We know commenting is very important when coding, and mod_rewrite also gives the possibility to comment while building your .htaccess file. You can comment by starting a "//" and whatever follows will be commented and will not be run by the RewriteEngine.
Example
RewriteEngine off
// the following code will not be parsed by the mod_rewrite engine
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]
// after RewriteEngine on, any following mod_rewrite code will be parsed
RewriteEngine on
You can /........./ for a block of comments.
mod_rewrite Links
So here is about thinking about how your visitor will identify your website pages and how Apache can help rewrite the URL. Since you manipulated your links to be user-friendly, no one can understand how you created and formatted them.
Relative Links Are Missing!
If you notice when you test the user-friendly URL, the designs of the website are distorted. It is not our fault, Apaches mod_rewrite thinks that the script is in a subdirectory. to solve this problem, we will be advisable to use a relative link or used the HTML <\base> tag
<head>
<\!-- ... head tags withOUT relative links ...-->
<base href=\"http://www.domain.com/updates.php\" />
<\!-- ... head tags with relative links ... -->
</head>
Note you can use an absolute link(that adds / to the full URL), which I can remember using sometimes to solve this problem.
I think we touch every aspect under mod_rewrite, and let look at possible practical examples and where we could make use of mod_rewrite.
Replace A Character
We are looking at characters like dash, and which can sometimes cause a problem especially when getting or sending to a database. So with the regex, we can change this character to underscores given that we don't know the number dashes in our URL.
RewriteEngine on
RewriteRule ^(.*)-(.*)$ $1_$2 [N,L]
Our example, made of two atoms separated with a dash, and that is why you see $1_$2, replacing them - with an underscore.
Unlimited key/value pairs
We have been talking about the number of pairs from "$1" to "$9" so far, let's look at a situation of Unlimited key/value pairs., So using the Next Flag will help us to change an unlimited number of -'s to _'s.
RewriteEngine on
RewriteRule ^([a-z]+)/([a-zA-Z0-9_]+)/((.+)/)*redirect\.php$ $3redirect.php?$1=$2 [QSA,N,L]
RewriteRule will capture a new key/ value pair with two atoms($1) and $2 and anything coming after the will be the $3 including "/" which redirect this leftover with the redirect.php script. If you do not want the redirect script in the URL, you need to think of another option.
RewriteEngine on
RewriteRule ^([a-z]+)/([a-zA-Z0-9_]+)(/(.*))*$4/?$1=$2 [QSA,N,L]
RewriteCond %{QUERY_STRING} =
RewriteRule ^$ redirect.php [L]
Another case is where we have to capture the first two atoms and the third to capture anything. So you see after the first two, we can add an unlimited number of key/value pairs.
WARNING: Do NOT go more than 255 characters in your URI
Force www for a Domain
It's the case that we want to force our browser to use the www. prefix to the domain name. As we did a test for mod_rewrite, let test the Apache {HTTP_HOST} variable to check.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule .? http://www.domain.com%{REQUEST_URI} [R=301,L]
If in case we have a subdomain,
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1domain.com%{REQUEST_URI} [R=301,L]
Eliminate www for a Domain
Here we just look at the opposite of what we're doing above.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule .? http://domain.com%{REQUEST_URI} [R=301,L]
what if you work with a subdomain name, you want to get off the www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?domain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
Redirect to a 404 Page
In case your host doesn't have a "file not found" redirection, you can create it for your site.
RewriteEngine on
// not a file
RewriteCond %{REQUEST_FILENAME} !-f
// not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /404.php [L]
So far this script will do two checks, the first will check if the request filename exists, the next will check if your request is a directory. If not, it will redirect to the document root's 4040.php. Adding ?url=$1 immediately after the /404.php will extend by including the URL.
RewriteEngine on
// not a file
RewriteCond %{REQUEST_FILENAME} !-f
// not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /404.php?url=$1 [L]
Rename Your Directories
As we saw from the example above, we used the .htaccess files to change the content in the test.html URL. So is the case where you want to manipulate your site by changing your directory names.
# mod_alias can do this faster without the regex engine
RewriteEngine on
RewriteRule ^old_directory/([a-z/.]+)$new_directory/1 [R=301,L]
Here we have the dot character to allow file extensions and the a-z to allow only lowercase characters in the range.
Convert .html Links to .php Links
We did a sample of this above and we all saw how this works, it is the case you want to apply a particular extension to all links. Note this is not a permanent redirection, it is just not visible to the visitor.
RewriteEngine on
RewriteRule ^([a-z/]+)\.html$1.php [L]
To make it permanent and visible to the visitor, you have to change the flag and used [R=301, L]
Extensionless Links
It is this script I have been using for the past. It makes the URL easy to remember by the visitor. In case a developer wants to hide the files extension, this is also a good Mod_rewrite script to use.
RewriteEngine on
RewriteRule ^([a-z]+)$1.php [L]
It will only work if you are using either .html or .php files. What if you are using both on the same project, you will need to use a RewriteCond statement to check the filename of either extension.
RewriteEngine on
# Test php first - gives preference in case you have both
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9]+)$1.php [L]
# then test for the .html extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([a-zA-Z0-9]+)$1.html [L]
Note, I have a -f is added to the RewriteCond, whose purpose is to check if the for exists.
Enforce Secure Server
Apache can check if you are using a secure server or not, using the {HTTPS} and {SERVER_PORT}.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^secure_page\.php$
RewriteCond %{HTTPS} !on
RewriteRule ^(secure_page\.php)$ https://www.domain.com/$1 [R=301,L]
OR
RewriteEngine on
RewriteCond %{REQUEST_URI} ^secure_page\.php$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(secure_page\.php)$ https://www.domain.com/$1 [R=301,L]
Note we have 443 because it is the post for secure servers. So when the HTTPS variable is null, the next option s is {SERVER_PORT} to request a secure server.
Conclusion
So far I might not have touched every aspect of mod_rwrite, but as I mentioned above, it used to allow Search Engine Optimization? User-friendly URL. It is a flexible tool that webmasters can use to apply important redirection tasks on their sites. The regex is tailored to mod_rewrite and apache variables and Flags. Don't make a habit of using NoCase Flags in a RewriteRule, only used to match case Insensitive characters in the {HTTP_HOSt], and last, always remember the last Flag. if I did not mention, the start anchor depends on the version of Apache, that is :
^/ for Apache 1.x
^ for Apache 2.x
^/? for both.
So it will all depend on your Apache version of simply using the third option.
Thanks for reading, im open for questions
good night, you as the Greater Helper I ask for help to verify my Achievment 5 Task 4 for verification immediately, here is the link
https://steemit.com/hive-172186/@aryadwigantara/achievement-5-task-4-by-aryadwigantara-review-steemdb-io
Immediately, slow down
This post has been rewarded by @saxopedia from @steemcurator04 Account with support from the Steem Community Curation Project."
Keep posting good content and follow @steemitblog for more updates. Keep on steeming as well as improving your content to get more attention. Thank you, Steemit Team!
https://steemit.com/hive-172186/@usmanali1/achievement-6-task-by-usmanali1-understanding-curation-and-community
Verify my achievement 6
Please Verify my Achievement 1, so that I can continue on the next assignment
https://steemit.com/hive-172186/@robbeert/achievement-1-introduce-my-self
https://steemit.com/hive-172186/@dani303/2ytgc7-my-achievement-i-introduction-myself-dani303
Please verify introduction my achievement