.htaccess RewriteCond small issue

thanks to @Ωmega, I almost completely resolved my former issue (details here .htaccess regular expression issue) and came to this solution :

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} ^(.*/|)(en|de|fr)(/|)$ [NC]
RewriteCond %1%3index.php -f
RewriteRule ^(.*/|)(en|de|fr)(/|)$ index.php?lang=$2 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} ^(.*/|)(en|de|fr)(/.*|)$ [NC]
#RewriteCond $1$3.php -f
RewriteRule ^(.*/|)(en|de|fr)(/.*|)$ $1$3.php?lang=$2 [NC,QSA,L]

Basically, these lines will rewrite anything in the form of domain/language/sub/.../somepage to domain/sub/.../somepage.php?lang=language. The first block is for urls in the form of domain/language/, with or without an ending / which is redirected to index.php.

This works very well tho two small glitches remain which are :

  1. The 3rd line in the second block (I #commented it) just doesn't match, no matter what. The strange part is that it DOES work on the 4th line, ie my page actually displays fine. Does that mean the -f parameter is wrong here ?

  2. If I input any url ending with a / (second block only, it DOES work with the index file), my rewrite is broken ie outputs as domain/language/sub/.../somepage/.php Is there any way to exclude a potential / in the inputed url with the regexp ?

Thank you for your feedback !

Answers


The 3rd line in the second block (I #commented it) just doesn't match, no matter what. The strange part is that it DOES work on the 4th line, ie my page actually displays fine. Does that mean the -f parameter is wrong here ?

You are using the wrong backreferences. You want %1 and %3:

RewriteCond %1%3.php -f

If I input any url ending with a / (second block only, it DOES work with the index file), my rewrite is broken ie outputs as domain/language/sub/.../somepage/.php Is there any way to exclude a potential / in the inputed url with the regexp ?

Yes, you can try changing your regexp from ^(.*/|)(en|de|fr)(/.*|)$ to ^(.*/|)(en|de|fr)(/.*?)/?$ in both the RewriteCond and RewriteRule.


Need Your Help

Updating UI after multiple asynchronous completion handlers complete

ios objective-c xcode

I need to make 12 requests to the same API (the data changes). Every time I make a request, I want to add the retrieved data to my array. The way I know how to do this is known as callback hell. It...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.