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.
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 !
2
Answers
I added two conditions (the first is to apply according to local or live site, the second to leave the node paths unchanged) :
And it is working as expected. Thanks again.
In
.htaccess
, the URL-path matched by theRewriteRule
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 matchdE
,Ed
orDE
.For example (near the top of the root
.htaccess
file):(HTTP, not HTTPS?!)
The trailing
$
on theRewriteRule
pattern was also redundant.Test first with 302 (temp) redirect to avoid potential caching issues.
The
Host
HTTP request header (ie. the value of theHTTP_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.Careful with testing 301s since they are cached persistently by the browser. You will need to clear your browser cache before testing!