2

I'm trying to redirect pages in a way that the language indicator, which in the URL is expressed as a subfolder ("en/", "de/" ...), is converted to a parameter I can work with. "FR", as the site's main language, does not have a language subfolder.

To illustrate this:

www.domain/index.html  

should redirect to

www.domain/index.php?lang=fr

and

www.domain/en/index.html 

should redirect to

www.domain/index.php?lang=en

I've tried the following in my .htaccess file (which is located in the root directory):

RewriteEngine On

RewriteRule ^en/index\.html$ /index.php?lang=en [L]   
RewriteRule ^de/index\.html$ /index.php?lang=de [L] 
RewriteRule ^index\.html$ index.php?lang=fr [L]

The last one works as expected, however the first two return a 404. For the second line ("de version"), I get:

The requested URL /de/index.php was not found on this server.

Apache logs the following:

[Thu Jul 09 13:14:28 2015] [error] [client ::1] script '/Applications/MAMP/htdocs/testsite/de/index.php' not found or unable to stat

It would appear that the first two rules are completely ignored by the server, as changing the target file name in the second line as below:

RewriteRule ^de/index\.html$ /blah.php?lang=de [L]

still triggers the same 404 message stating the missing de/index.php and not blah.php ;

What does work however is placing a .htaccess in a subfolder named 'de' and pointing from there to the index.php in the root folder with

RewriteRule ^index\.html$ /index.php?lang=de [L]

But that's not a proper solution, is it ?

Argoron
  • 741
  • 2
  • 10
  • 26
  • Are these really all the rules? Which path does the `error.log` document? Enable rewrite logging. – mario Jul 08 '15 at 14:35
  • 1
    i see the only problem - add leading slash in the 2nd rule `RewriteRule ^index\.html$ /index.php?lang=fr [L]` – splash58 Jul 08 '15 at 14:41
  • */en/index.php* - who has changed extension from `html`? – splash58 Jul 08 '15 at 14:51
  • RewriteRule ^(en)/(index\.html)$ index.php?lang=en [L] – Eric Jul 08 '15 at 14:58
  • @Eric ok. and who return ^/en/ ? I think that there are more lines in htaccess – splash58 Jul 08 '15 at 15:00
  • @splash58 Could you please explain what the use of changing the second rule, which works correctly, would be? – Argoron Jul 08 '15 at 15:02
  • @Argoron i don't understand what is happened with input url that /en/index.HTML became /en/index.PHP No rule, making so, in this code – splash58 Jul 08 '15 at 15:05
  • Do you have any other rules there? – Amit Verma Jul 08 '15 at 15:07
  • @Starkeen Not yet because I've only just started rebuilding the whole thing. – Argoron Jul 08 '15 at 15:11
  • Clear your browser's cache, may be it's because of that. – Amit Verma Jul 08 '15 at 15:14
  • @mario Done. I edited the question accordingly. – Argoron Jul 09 '15 at 11:27
  • Maybe one of those rare cases where `RewriteBase /` may help. Due to potential overlap even `Options -MultiViews`. And the target rule typically goes better without `/` prefix in `/index…?…` – mario Jul 09 '15 at 11:35
  • @mario I tried RewriteBase early in my attempts to make this work but to no avail. Also just tried Options -MultiViews and removed the leading slashes. Oddly, these "en" and "de" rules still do not get applied, the default one (no subdirectory) though does. If I change the target file name to blah.php for the default rule, I get an appropriate error message ( blah.php not found), whereas for the other two it's /xx/index.php not found ... – Argoron Jul 09 '15 at 12:29
  • At this point you should really enable and inspect the RewriteLog. At the least also prevent multiple rounds using `[END]` instead of `[L]`, because this still reads like interacting rules. Existing `en/` or `de/` subdirs should be absent for further tests. – mario Jul 09 '15 at 12:39
  • See also [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/a/31280108) (sorry for the length), [Hidden features of mod\_rewrite](http://stackoverflow.com/q/286004), and lastly [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/q/214512) for additional tips – mario Jul 09 '15 at 12:41

1 Answers1

0

In some cases, adding this line before the rules helps:

RewriteBase /

Also, in some server configurations, you need to start with ^/ instead of just ^. So a rule would look like this: RewriteRule ^/en/index.html /index.php?lang=en

This solved it once for me.

Just a tip:

Both EN and DE rules can be written as a single rule like this:

RewriteRule ^(en|de)/index.html /index.php?lang=$1

This can also be expanded to as many locales as you want, just separate them with a | symbol. | is basically an OR operator.


I've tested these and they seem to do what you need:

RewriteRule ^index\.html /index.php?lang=fr [L]

/index.html -> /index.php?lang=fr


RewriteRule ^en/index\.html /index.php?lang=en [L]

/en/index.html -> /index.php?lang=en


RewriteRule ^de/index\.html /index.php?lang=de [L]

/de/index.html -> /index.php?lang=de


Another, less limited solution would look something along these lines:

RewriteRule ^([a-z]{2})/index.html /index.php?lang=$1

/xx/index.html -> /index.php?lang=xx

This actually takes any two characters and sets them where the $1 is. But this might not be what you really want, it's just a tip.


P.S. This is a nice tool for testing rewrite rules: LINK

Janis Vepris
  • 577
  • 3
  • 13