mod_rewrite one domain to another while preserving URL for bilingual site - mod-rewrite

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]

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.

.htaccess image to web page redirect of sorts

I think this can be done, though not sure, so I came here to find out from you experts. I researched extensively, but always get the opposite of what I want to do. I was wondering if someone types in the image file URL (on server side) into an address bar if that could be directed to its corresponding web page.
For example:
One types in yourdomain.com/wp-content/uploads/image-name.jpg and it's directed to yourdomain.com/print/image-name (w/o .jpg)
So instead of one just seeing the image in a window, they will see the image, it's information and how to purchase it on my brand web site.
If that is not possible or the file name doesn't match, can directly typed server side image file URLs redirect to my website galleries or home page? And where in .htaccess would this code be located? Above begin wordpress or not?
If above Begin WP, will that interfere with this code that already exists? If so how would I exactly write it?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sitename.com/$1 [R,L]
Thanks in advance!
EDIT: To clarify, my site being down has nothing to do with the help I received here. I upgraded my web host account and there was a glitch when they migrated it.
Insert this rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /wp-content/uploads/(.+?)\.jpg [NC]
RewriteRule ^ /print/%1? [L,R=302,NE]

How to 301 alternative language sites to english?

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

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]

How to deal with "#" in a query string in mod_rewrite?

I asked this question about rewriting old "ugly" links into seo friendly ones.
I need to "extract" some information after the "sharp" symbol in some urls. I've created a regular expression to it but it can't seen to work.
After my question, I created this logic for this url for example, script.php?mode=full&id=23:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /script\.php\?mode=full&id=([0-9]+)\ HTTP/
RewriteRule ^script\.php$ fix_old_urls.php?phpfile=script2&id=%1 [NC,L]
But I'm working in a legacy application, and I need to extract the value after the sharp symbol in some pages, in the example url script.php?mode=full#23:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /script\.php\?mode=list\#([0-9]+)\ HTTP/
RewriteRule ^script\.php$ fix_old_urls.php?phpfile=script&id=%1 [NC,L]
(in fix_old_urls I properly redirect with a 301 code).
The first one works, but not the second. To me it looks like it`s the same logic in both. What am I'm doing wrong?
Anchor information (the part starting with the #) is never actually sent to the server. It's handled completely by the browser, and thus you can't touch it with mod_rewrite (nor can you access it via server-side scripting languages). The only things that can see it are the browser and client-side scripts (like Javascript).

Resources