.htaccess - Redirect domain/lang/ to subdomain for each language - apache

I've been struggling with this for weeks and I can't get to the solution, I have to make a redirect in .htaccess of a multi-language website with all its pages following the same structure but with a subdomain for each language. In the following way that is valid for http as https:
domain.es/es/allpages to es.subdomain.com/allpages
domain.es/pt/allpages to pt.subdomain.com/allpages
domain.es/de/allpages to de.subdomain.com/allpages
domain.es/fr/allpages to fr.subdomain.com/allpages
domain.es/en/allpages to eu.subdomain.com/allpages
Any ideas?
I have tried the following but not sure if it is correct:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) https://es.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/pt/(.*)$
RewriteRule ^(.*) https://pt.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/fr/(.*)$
RewriteRule ^(.*) https://fr.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/de/(.*)$
RewriteRule ^(.*) https://de.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/en/(.*)$
RewriteRule ^(.*) https://eu.subdomain.com/%1 [R=301,NC]
Thanks a lot!!!!

Based on your shown samples, could you please try following. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.es$ [NC]
RewriteRule ^(es|pt|de|fr|en)/(.*)/?$ http://$1.subdomain.com/$2 [NE,R=301,NC,L]

Related

Wildcard in Modrewrite htaccess APACHE redirection

In my .htaccess APACHE's file I have the following code:
#MY AWESOME WEBSITE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^myawesomewebsite.com
RewriteRule ^(.*) http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website [P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.myawesomewebsite.com
RewriteRule ^(.*) http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website [P]
Both, myawesomewebsite.com and coolwebsite.com are in my hosting linked properly, myawesomewebsite.com is an alias.
That code works fine. But IF I go to:
myawesomewebsite.com/index.php
myawesomewebsite.com/wtf.php
myawesomewebsite.com/blahblahblah
myawesomewebsite.com/whateveryouwrite
You will see the content of coolwebsite.com/index.php in all cases.
I want redir: .*myawesomewebsite.com.* to http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website
How can I achieve this?
Thanks for reading, and please forgive my bad english.
You are using the [P] flag, which is for proxying and not what you want. Replace all your rules with only this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?myawesomewebsite\.com$
RewriteRule ^ http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website [R=301,L]
I took out the condition to only redirect if the file doesn't exist, since you didn't say you wanted it to work that way.
Let me know if I misunderstood what you're trying to acheive.
Update
Try this instead:
RewriteCond %{HTTP_HOST} (?:^|\.)myawesomewebsite\.com$
RewriteCond %{REQUEST_URI} !=/web/this-is-a-friendly-url-version-of-my-awesome-website
RewriteCond %{REQUEST_URI} !=/web.php
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule ^ /web/this-is-a-friendly-url-version-of-my-awesome-website [L]

.htaccess dynamic subdomain files

here is my code in my .htaccess file:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com [NC]
RewriteRule ^/?$ /user/profile.php?name=%1 [L]
here is what it does:
if i visit test.domain.com, it will show the contents (not redirect) of domain.com/profile.php?name=test.
but if i type: test.domain.com/login, it will show the contents of the page: domain.com/login.
Question: why is that? how can i turn that off? thanks
Hope this will work, working fine on my end.
RewriteCond %{HTTP_HOST} !^www\.domain\.in
RewriteCond %{HTTP_HOST} ^test\.dmmain\.in [NC]
RewriteRule ^$ http://test.domain.in/user/profile.php [L,QSA]

htaccess condition and rewrite

i have this link
http://localhost/fukhtaccess
and i want redirect the link to http://server.localhost/index.php with GET params.
this is my htaccess, but don't work and not possibile test with some error for debug (thanks apache foundation!!!)
RewriteEngine On
RewriteCond %{HTT_HOST} ^localhost$
RewriteCond %{REQUEST_URI} fukhtaccess
RewriteRule (.*) http://server.localhost/index.php [QSA,L]
You have a typo: change HTT_HOST to HTTP_HOST.
Also, your second RewriteCond just complicates things unnecessarily. Do this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule fukhtaccess http://server.localhost/index.php [QSA,L]
better, this work fine
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} fukhtaccess$ [NC]
RewriteRule (.*) http://server.localhost/index.php [QSA,L]

Need help removing special characters rewrite htaccess

Struggling with a 301 redirect in htaccess after changing my links on my blog.
I have the following code in htaccess today:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^sitemap.xml$ sitemap.php [L]
Rewriterule ^blog/(.+)/(.+).html$ ./blog/view/blog.php?page=$1&mode=$2 [NC]
Rewriterule ^blog/(.+).html$ ./blog/blog.php?page=$1 [NC]
Rewriterule ^blog/(.+)/$ ./blog/view/blog.php?page=$1 [NC]
Rewriterule ^blog/$ ./blog/blog.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
Rewriterule ^blog/(.+)/(.+)$ ./blog/view/blog.php?page=$1&mode=$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.+)$ ./blog/blog.php?page=$1 [NC]
The problem is that I´ve now in php converted special characters like 'åäö' to 'aao' but the old links to my old posts has no redirect to the new links.
Can someone please help me out here.
Here is an example of a simple 301 Redirect that I based the following code from.
Here is an example of redirection based on extension to fit your requirement.
RewriteEngine On
RewriteCond %{REQUEST_URI} .html$
RewriteRule ^blog/(.*).html$ /blog/blog.php?page=$1[R=301,L]

Redirect not working for complex rewrite rules (.htaccess)

I'm trying to make a redirect from a non-www version of the link to the www one. Its working fine for something like http://mywebsite.com but it fails for a request like http://mywebsite.com/artists/metallica/ or even a complex one. The whole .htaccess file is bellow. Any clues?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*).html
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mywebsite.com/$1/ [L,R=301]
RewriteRule ^artists/([^/-]+)-p([^/]+)/$ /artists.php?l=$1&p=$2 [QSA,L]
RewriteRule ^artists/([^/]+)/$ /artists.php?l=$1 [QSA,L]
RewriteRule ^submit/$ /submit.php [QSA,L]
RewriteRule ^users/$ /users.php [QSA,L]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mywebsite.com/$1 [R=301,L]
Try this rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But make sure that you put this rule in front of those rules that just do an internal rewrite. Otherwise an already internally rewritten rule might get redirected externally.