简体   繁体   中英

.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/.../ Is there any way to exclude a potential / in the inputed url with the regexp ? 是否有任何排除方法一个潜在的/在正则表达式在输入的网址?

Thank you for your feedback !

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM