How to 301 alternative language sites to english? - mod-rewrite

My client has a website that has english, german, spanish and french sites. They are just simple folder structures on the server inside the public_html directory eg. /de, /fr, /en and /es
They have decided to remove the other languages and only have english but i have quite a few pages that need 301 redirecting.
I already use a mod_rewrite for doing pretty URLs throughout.
What would be the best way to redirect the alt language pages to the english pages without disrupting the pretty URLs?
eg. fr/news/news-article -> en/news/news-article
I already have:
RewriteRule ^([^/\.]+)/news?$ $1/news.php [L]

Add this RewriteRule line on top of all other Rewrite rules in your .htaccess:
RewriteRule ^(?:de|fr|es)(/.*|)$ /en$1 [L,R=301,NC]
This will redirect (with R=301) every /fr/bla/bla or /de/bla/bla or /es/bla/bla to /en/bla/bla

Related

Apache RewriteCond not working for static files

I have two copies of the website in separate directories for different languages (Spanish and English) and the directory structure is as follows
+ /var/www/website
|- es
|- en
The es directory serves the Spanish version of the website and en serves the English version (default language).
The URL schema would be like
# English version
https://example.com/
https://example.com/en/ -> Redirects to https://example.com/
# Spanish version
https://example.com/es/
The static files are served from the respective directories only.
Now, I have the following Apache2 configuration
<VirtualHost *:443>
# The primary domain for this host
ServerName example.com
DocumentRoot /var/www/website
<Directory /var/www/website>
Require all granted
AllowOverride all
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^$ /es/ [R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [R]
RewriteCond %{HTTP:Accept-Language} !^en [NC]
RewriteCond %{HTTP:Accept-Language} !^es [NC]
RewriteRule ^$ /en/ [R]
</Directory>
</VirtualHost>
I'm facing a few problems with the configuration
https://example.com/es/ and https://example.com/en/ are working but static files are not loading and the URL for the static files looks like https://example.com/image.png which has to be https://example.com/es/image.png (for Spanish) and https://example.com/en/image.png (for English)
https://example.com/en/ should be redirected to https://example.com/ and the English website should be served, whereas the reverse is happening.
Let's first understand what is happening here.
With rules like this
RewriteRule ^$ /es/ [R]
you are redirecting requests to / to /es/.
Redirecting means, the server will tell the browser to go search for the content at some other place.
The browser will then send another request to /es/. This will not be rewritten nor redirected as there is no matching rule.
You html file apparently contains references with absolute URLs like this:
<img src="/image.png" />
Your browser will therefore send a request for /image.png, which is not redirected, nor rewritten.
There are different ways how to solve that:
1. Update your html/code to absolute URLs with the right path
Like this:
<img src="/es/image.png" />
That's the best solution imho. Depending on how you generate the content, there might be an option to set the base URL of the project.
1b. Make the webserver do these substitution on the fly for you
You could think about whether you want apache to make these substitutions for you, e.g. using mod_substitute, but there might be cases you cannot catch with this approach. I would not recommend it.
2. Update your html/code to relative URLs
I.e.
<img src="image.png" />
This sounds nice on first sight, but can become ugly if you're having complex page structure, e.g. /es/foo/bar/somepage.html would need <img src="../../image.png" /> to reference /es/image.png.
It can become especially tricky if you want to serve the same content under /es/foo and /es/foo/ (without redirecting one or the other). The two would need a different relative path to the image.
3. Rewrite requests for /image.png
In theory, you could let Apache rewrite requests for /image.png to either /es/image.png or /en/image.png depending on the accept-language header.
I would not recommend that, as it leads to strange behavior. E.g. assume a user consciously decides to visit /en/ even though his language settings prefer Spanish. The browser would then request /image.png and the server would deliver - based on the language settings - the Spanish variant of the image. While the HTML shows the English content.

How to setup a 301 redirect using mod_rewrite

I must admit, mod_rewrites are still a bit of black magic for me and I try to stay well away from them unless I really, really need to us them, now I do!
I've just migrated my site to become a multi lingual site and in doing so I've made the following change to the structure.
oldwebsite.net/somefile.php >> newwebsite.com/xx/somefile.php
oldwebsite.net/somefolder/somefile.php >> newwebsite.com/xx/somefolder/somefile.php
Now, I know how to do individual 301 redirects, but I got tons of pages and the new structure hasn't changed the page name so I would like to avoid setting up hundreds of individual 301 redirects, help!!
Thanks,
Mikael
You can put this code in your oldwebsite htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} oldwebsite\.net$ [NC]
RewriteRule ^(.*)$ http://newwebsite.com/xx/$1 [R=301,L]

Using mod_rewrite or htaccess to apply 301's to entire site structure, with URL's built using query strings

I am taking over an old website, and need to change the domain name.
The current domain is http://www.example.com/folder/ and the new domain is http://example.school.nz
An example of an existing and desired URL is:
Before: http://www.example.com/folder/index.php?page=sport
Desired: http://example.school.nz/about-us/sport
The site is built using CMSMS (http://www.cmsmadesimple.org), so pages and URL's are generated through the CMS, although it's easy enough to export a list of URL's.
Ideally I could set up 301 redirects for the entire site so the user doesn't get 404'd. Any clues to the easiest way to accomplish this?
It depends on how general you want things to be, but this set of rules will do what you asked in your question:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} page=sport
RewriteRule ^folder/index.php http://example.school.nz/about-us/sport? [L,R=301]

mod_rewrite one domain to another while preserving URL for bilingual site

I have two domains: myenglishdomain.com and myfrenchdomain.com. They both point to the same folder on my server (the english domain). The way I've structured the server is that the english content is at myenglishdomain.com/en and my french content is at myenglishdomain.com/fr (note: my english domain, not the french one).
when a user types in myfrenchdomain.com/content I'd like the URL to still read myfrenchdomain.com/content, and yet point to myenglishdomain.com/fr/content
is this possible? What would be the rule(s) to make this happen?
RewriteCond %{HTTP_HOST} ^myfrenchdomain.com
RewriteRule ^/content/(.*) http://myenglishdomain.com/fr/content/$1 [P]

For SEO URLs via .htaccess do extensions need to be 301'd as well?

After researching, I have come to some conclusions for utilizing SEO URLs. For semi-static .php based sites that have file names such as index.php, about.php, contact.php etc I am using htaccess mod_rewrite rules so that for example 'www.mysite.com/about' can be used in place of 'www.mysite.com/about.php'. In such cases, all of my menus and links are then pointing at the SEO URL.
What I have found out though, is that even though nothing in my site references the .php extensions, if I enter them directly, they stick. So, I can get to the about page using /about or /about.php, and each will show as entered. It seems reading through posts here that this is a common issue.
My question is, does this matter? If nothing in my site references the .php extensions in a link or menu, do i need to be worried about duplicate content??
I was told I need to add something in htaccess like:
redirect 301 /about.php http://www.mysite.com/about
for each page to eliminate the possibility but I don't understand if this is really an issue in this case?
Two Questions:
Q1. "Does this matter"?
Answer: Not if you've never exposed the .php URLs before. If you have, then the answer changes to "yes".
Q2. What .htaccess?
Answer:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R=301,L]
Your URLs are SEO friendly if there are some keywords in it that relate to the content and if they are not overly dynamic in a sense that Google Bot can interpret them (take a look at the Google Webmastertools and read this).
So I would not worry about "mysite.com/about" versus "mysite.com/about.php". I would rather watch out to avoid duplicate content if you want to change the URLs.
Here are some more links for further reference:
Making Website URLs SEO Friendly and Pretty
Some articles and advice on Duplicate Content

Resources