.htaccess file can add some elegance to what can be sometimes long and clumsy urls. It must be written as text file (not forgetting the preceding full stop) and generally saved in the root directory of your domain with no suffix.
An excellent resource for getting to grips with .htaccess…
2020 Complete Guide to .htaccess
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mastertemplate.co.uk$
RewriteRule ^(.*)$ http://www.mastertemplate.co.uk/$1 [R=301,NC,L]
RewriteRule ^$ /home [R=302,NC,L]
RewriteRule ^home$ index.php [NC,L]
RewriteRule ^htaccess$ htaccess.php [NC,L]
The above file is designed to rename the page url’s from index.php to home etc.
Look at the url in the browser now! You should see a neat url http://www.mastertemplate.co.uk/htaccess
Not having a suffix of i.e. .php or .htm etc.
NB. With your .htaccess file in place remember to rename each of your hyperlinks in your website from ‘index.php’ to ‘home’ etc. Otherwise the hyperlinks will point directly to your .php files and load them so you won’t see your neat abbreviated urls.
[NC] No Case. Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn’t care whether letters appear as upper-case or lower-case in the matched URI.
[L] Last. Use this flag [L] to indicate that the current rule should be applied immediately without considering further rules. Similar to what ‘;’ does for a single PHP statement or } for a block statement.
My example above is for using .htaccess to provide elegant Url’s. But the primary function of the .htaccess file is Redirection.
A handy website offering 10 things you should know about .htaccess Rewrites is 10 Mod Rewrite Rules you should know.
In the Rewrite rule below any request for an image file will be proxied to your dedicated image server. The match is case-insensitive, so that .jpg and .JPG files are both acceptable.
RewriteRule (.*\.(jpg|gif|png))$ http://images.example.com$1 [P,NC]
Use of the [P] flag implies [L] - that is, the request is immediately pushed through the proxy, and any following rules will not be considered.
You must make sure that the substitution string is a valid URI (typically starting with http://hostname) which can be handled by the mod_proxy. If not, you will get an error from the proxy module. Use this flag to achieve a more powerful implementation of the ProxyPass directive, to map remote content into the namespace of the local server.
Scenario: We want direct visitors to our .com website to our .co.uk website or vice versa.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?yourwebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.yourwebsite.co.uk/$1 [R=302,L]
With this .htaccess code anything with “yourwebsite.com” in the uri gets replaced with “yourwebsite.co.uk” this means that even “yourwebsite.com/another/directory/awebpage.html” will redirect nicely to “yourwebsite.co.uk/another/directory/awebpage.html” etc.
The above .htaccess Rewrite Condition Rewrite Rule code is set to [R=302,L] (Rewrite R=302 which is the default for Rewrite Rules so could in this instance be left out. This code No. denotes a “Temporary” Redirect.
The request has more than one possible set of responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses.
Remove a page completely but forward to another page.
You may wish to move a URL temporarily to another URL without upsetting the listing & SEO page rank within search engine results as you intend to move the URl back to it’s original location in the near future
Additionally: Prevents bookmarking or refreshing of pages that hold one-time only data like credit card payment information (Any/all information entered within a form). The server may well be set-up to redirect you to another page and carry across the submitted details.
NOTE: The difference between 301 and 302 Redirection.
When a web crawler encounters a link to your site whose URL is configured with a permanent redirect, your (Apache) web server responds with a 301 status code and then redirects the crawler to the new URL. The search engine not only accepts the redirection to the new URL but also begins the process of transferring any existing page rank value from the old URL to the new one. It’s the second part of this process that’s so important for SEO which simply isn’t done with a 302 Rewrite Rule.
Redirection and transferring the data from a form input across to the newly redirected page. Information sent using the POST method. Information retrieved using the GET method.
Identical to that of 303 but without changing the way the information was transferred from one technique to another! The same technique is used.
Same as 307 but set permanently
“300” Rewrite code definitions from Mozilla org.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
An excellent resource for getting to grips with .htaccess…
2020 Complete Guide to .htaccess
Please share if you find the content useful - thank you
FORUM |
|
Have a comment or something to say? |