RewriteRules can be such a pain for me, I cannot get this one to work. I have to redirect urls like example.com/de/anypath to example.de/anypath.
[anypath] can be really any path, as I have to get it work for example.com/de/articles/programming/hello-world (would be redirected to example.de/articles/programming/hello-world) as well as for example.com/de/events/pic-nic (would be redirected to example.de/events/pic-nic).
This is what I wrote so far :
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
I also tried with RewriteCond with no more luck
RewriteCond %{HTTP_HOST} http://example.com/de
I am working with xampp, but tested on my web server with same result.
I know this
.htaccess
file is working (get error if I enter a typo)I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Any help would be appreciated !
CodePudding user response:
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
In .htaccess
, the URL-path matched by the RewriteRule
pattern does not start with a slash. ie. It should be "^de/(.*)$"
, not "^/de/(.*)$"
.
You don't need the double quotes and the NC
flag is probably redundant, unless you also need to match dE
, Ed
or DE
.
For example (near the top of the root .htaccess
file):
RewriteRule ^de/(.*) http://example.de/$1 [R=301,L]
(HTTP, not HTTPS?!)
The trailing $
on the RewriteRule
pattern was also redundant.
Test first with 302 (temp) redirect to avoid potential caching issues.
RewriteCond %{HTTP_HOST} http://example.com/de
The Host
HTTP request header (ie. the value of the HTTP_HOST
server variable) contains the hostname only. eg. example.com
only in your example.
Any server variable that is prefixed with HTTP_
refers to the HTTP request header of the same name.
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Careful with testing 301s since they are cached persistently by the browser. You will need to clear your browser cache before testing!
CodePudding user response:
I added two conditions (the first is to apply according to local or live site, the second to leave the node paths unchanged) :
RewriteCond %{HTTP_HOST} local.example.com
RewriteCond %{REQUEST_URI} !^.*/de/node/.*$
And it is working as expected. Thanks again.