1

I'm trying to have a parameter that comes just after he hostname, as in:

www.domain.com/parameter

For this reason, I've set the following rewriteRule, for two parameter values that I'd like to operate with:

RewriteRule ^(en|pt)$ /index.php?language=$1&%{QUERY_STRING} [L]

Works fine! Although, I need to create a new rule, to redirect any request without one the parameters listed.

For that reason, I thought this would work:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^(en|pt)$
RewriteRule ^(.*)$ /pt/$1/ [L,R=301]

But unfortunately is showing a redirect loop.

I've been getting:

http://hostname/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt/pt//

Any tips on how to fix this are appreciated!

0

2 Answers 2

2

That's because directories are not regular files.

http://httpd.apache.org/docs/current/mod/mod_rewrite.html (no HTML DOM id for the snippet! closest one is an unrelated LA-U, even what should have been an id="LA-F" is missing!)

You can perform various file attribute tests:

'-d' (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
'-f' (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.

Provided the rest of your config is correct (I'm not sure adding a trailing slash is a good idea), you should add an extra condition for !-d:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(en|pt)$
RewriteRule ^(.*)$ /pt/$1/ [L,R=301]

Also, consider switching to nginx! Not only does it have a clearer syntax more geared towards common use, but it also has direct links to relevant documentation, like so -- http://nginx.org/r/if.

checking of a file, directory, or symbolic link existence with the “-e” and “!-e” operators;

2
  • Thanks for your attention, I really appreciate it. I've tried but without succcess. It's not redirecting it seems ( pastie.org/10475373 ). I'm expecting to get the domain.com/parameter (default), instead I get domain.com the request I've sent originally. @cnst
    – punkbit
    Oct 11, 2015 at 21:27
  • @punkbit, I don't know how that would be possible! (Perhaps you have to provide more details, or maybe ask a new question.)
    – cnst
    Oct 11, 2015 at 21:35
0

%{REQUEST_URI} is the URI path just following hostname, therefore, it starts with the forward slash: /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(en|pt)$
RewriteRule ^.*$ /pt/$0/ [R=301,L]

Not the answer you're looking for? Browse other questions tagged .