Anyone know their stuff when it comes to Rewrite rules in Htaccess?
-
Hi All,
I have the following code in one of our Htaccess files and I'm not entirely sure what its doing. Could anyone shed some light and maybe explain the process its going through? I know its something to do with redirecting the urls dependant on the browser language.
RewriteCond %{REQUEST_URI} ^/(En|Es)$ [NC]
RewriteRule ^(En|Es)/(.*)$ $2?lang=$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
Thanks
-
Hey David,
Sorry for the delayed response on this.
Before we get started, I should point out that htaccess's syntax is very particular and you should be extremely careful when messing with it. even a single space out of place can cause massive errors. If you're planning changes, please consult with a developer or three on your team!
I think the best way to explain this is to go through exactly what the htaccess Rewrite calls are doing.
RewriteCond %{REQUEST_URI} ^/(En|Es)$ [NC]
RewriteCond - The condition of which a rewrite will take place
%{REQUEST_URI} - The URI that is requested from the server (everything after the domain and TLD i.e. moz.com/community 's URI would be /community)
^ - Denotes the beginning of a regular expression (regex)
/ - literally just /
(En|Es) - the '()' are simply a grouping and the '|' means OR. So this is saying En OR Es
$ - Denotes the end of a regex
[NC] - Means no case, so everything in this is not case sensitiveSo literally this is saying execute this rewrite when the requested URI (after the .com or whatever TLD you use) is either /En OR /Es then whatever, with no attention to case
RewriteRule ^(En|Es)/(.*)$ $2?lang=$1 [L,R=301]
RewriteRule - The executed rule when the aforementioned RewriteCond is met.
^ - Denotes the beginning of a regular expression (regex)
(En|Es) - the '()' are simply a grouping and the '|' means OR. So this is saying En OR Es
/ - literally just /
(.) - This is a wildcard. Once again the () is a grouping, but here the . means zero or more arbitrary characters
$ - Denotes the end of a regex
$2 - this is the second captured grouping in this line. Meaning whatever is defined within (.*), which is everything after En/ or Es/
?lang= - this is literally writing '?lang=' without the 's.
$1 - this is the first captured grouping from this line. Meaning whichever En OR Es was captured will be written here.
[L] - Tells the server to stop rewriting after the preceding directive (rule) is processed
[R] - Instructs Apache to issue a redirect, causing the browser to request the rewritten URL
[301] - Corresponds to a Moved Permanetly Header Code
[L,R=301] - Combines all 3 of these into one.For this I think it's easiest to just use an example.
moz.com/En/htaccess-is-fun will be our example
Since this url passes the RewriteCond, it goes on to the RewriteRule where it finds En OR Es and stores that value as $1 (En) then takes whatever is left and stores it as $2 (htaccess-is-fun).
It then writes htaccess-is-fun?lang=En and replaces the original selection (which is En/htaccess-is-fun) with the new rewrite making the result moz.com/htaccess-is-fun?lang=En . The new URL is served as a 301-ed redirect.RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteCond - The condition of which a rewrite will take place
%{REQUEST_URI} - The URI that is requested from the server (everything after the domain and TLD i.e. moz.com/community 's URI would be /community)
! - declares negation. i.e. "!cheese" matches everything except "cheese"
() - is again a grouping
\ - escapes a special character. So "." means a literal dot.
a-zA-Z0-9 - matches all lowercase letters, all uppcase letters, and all numbers
{1,5} - matches one to five of the previous designation. Meaning that there can be any combination of a-z, A-Z, or 0-9 in a sequence of one to five. i.e. A2ps OR 12345 OR AbC etc.
| - Means OR
/ - literally just /
$ - Denotes the end of a regexRewriteRule ^(.*)$ $1/ [L,R=301]
^ - Denotes the beginning of a regular expression (regex)
(.) - This is a wildcard. Once again the () is a grouping, but here the . means zero or more arbitrary characters
$ - Denotes the end of a regex
$1 - this is the first captured grouping from this line.
/ - literally just /
[L] - Tells the server to stop rewriting after the preceding directive (rule) is processed
[R] - Instructs Apache to issue a redirect, causing the browser to request the rewritten URL
[301] - Corresponds to a Moved Permanetly Header Code
[L,R=301] - Combines all 3 of these into one.So whenever the RewriteCond is met, this rule will select everything and then rewrite it as a 301 with a / trailing it.
Hope this helps! Let me know if you have any other questions.
Regards,
Trenton
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Redirection Question - Can Anyone Help?
Hi Community, I have 2 job boards. Job board A is a .co.uk domain. Job board B is uk.com domain. Job board A displays jobs in the UK but has an international jobs section. Job board B focuses entirely on international jobs. To cut a long story short we are shutting down Job board B as we are going to just be using Job board A in future. In terms of redirection, would it be best to: 1. 301 redirect job board B domain to Job board A. (www.jobboardb.uk.com -> www.jobboarda.co.uk) or 2. 301 redirect job board b to the international jobs section on job board a (as this is the most relevant place for the user to go I am thinking to go with option 2, but I read somewhere that it wasn't best practice. Any help is much appreciated.
International SEO | | SO_UK0 -
My local traffic has increased but USA traffic decreased..I dont know why!!
Hi Guys, A sticky one for ye. I have recently updated my site selling Jewelry in April of this year. Overall, Local Irish organic traffic has been stronger than ever in the first half of 2015. Yet in the USA, since March it's been at it's weakest for years, declining month-on-month. In terms of Organic Traffic for May & June, there's been a 30% increase in visitors from Ireland when compared to last year. Yet for the USA - the biggest market for us- there has been a 39% decrease this year over the same dates. It's the same for language demographics. Big decrease for en-US, but increases for en-IE and en-GB. It's not just the US. The site has taken a hit in Canada, Australia etc.So domestically the website looks to be performing better than ever before since the HTTPS switch, but it has taken a big hit internationally in our key market.We are targeting USA in search console, not changed any of the content in the site however he have added an SSL cert to make the whole site HTTPS like google said :)Any pointers are welcome
International SEO | | Johnny_AppleSeed0 -
International advice.... can anyone help and check my site?
Hi ALL, I'm running 3 sites, internationally .com, com.au and co.nz Can anyone please look at my site and give me feedback about the hreflang tags, I ran a W3C and i have errors stating https://validator.nu/?doc=http%3A%2F%2Fzenory.co.nz for www.zenory.com and its relevant domains
International SEO | | edward-may0 -
Anyone knows/seeing this strange bump in traffic from Granada, Spain?
I have noticed a strange bump in traffic from August 14, coming from Granada Spain. I have a language specific website and there is no way I have suddenly thousands of readers in Granada. GA show the service provider is opera software asa and domain is opera-mini.net I am wondering the location shown in GA as Granda, Spain is the correct one. Perhaps Opera is proxying the traffic through it proxy server located in Granda. Or this is spammer trying to download content? Anyone else has seen this?
International SEO | | Maayboli0 -
Google adwords keyword tool - Can anyone recommend a free service that is similar?
Hi everyone, As many of you know the Google keyword tool is going away unless you have an active campaign. Can anyone recommend a tool that is similar and free? We do international SEO so it is really important for me to get keyword suggestions in several languages. Thanks Carla
International SEO | | Carla_Dawson0 -
Does anyone know where to find recently expired domains?
The title says it all. Is there anywhere I can find domains that are recently expired and back on the market? I'm thinking if the domain name is a good enough match and has had a reasonable authority that it may be worth buying and using in the correct environment...
International SEO | | Gordon_Hall0 -
Can anyone guide me best ways to generate customer leads through the SEO Process
I want to generate leads for my client through SEO process. I have involved all kind of link baits like articles, blogs, infographics, directory submissions etc.,Basically the client is B2B service provider like payroll services, Labor compliance and Staffing Solutions to various segments
International SEO | | Virrtuo0 -
Russian SEO: Do you know some good sources with tips and news about Yandex?
We are launching our site in Russia - and basically I have no experience in Russian SEO! Could you please recommend me some good English sources with news, tips and hints about Yandex? Basically I am looking the Russian SEOmoz 🙂 Thanks!
International SEO | | jorgediaz0