htaccess RewriteCond REQUEST_URI detection not working

三世轮回 提交于 2021-01-28 12:11:11

问题


I have two different things I want to happen in the case of two different types of URI's but I can't seem to stop the second case from executing where the first case has already been accepted.

Here's an example:

abc.com
abc.com/asdf
abc.com/en/asdf
abc.com/zh-cn/asdf/asdf

Should all be directed to this RewriteRule:

RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]

Part 2:

abc.com/myadmin/
abc.com/myadmin/asdf
abc.com/myadmin/en/asdf
abc.com/myadmin/zh-cn/asdf/asdf

Should all be directed to this RewriteRule:

RewriteRule ^myadmin/(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /myadmin/index.php?lng=$1&tpl=$4 [QSA,L,NC]

The way I've been attempting to acheive this is by stacking the conditions like this:

RewriteCond %{REQUEST_URI} !^/myadmin/
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]

RewriteCond %{REQUEST_URI} ^/myadmin/
RewriteRule ^myadmin/(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /myadmin/index.php?vlng=$1&tpl=$4 [QSA,L,NC]

But no matter what I do, both rules still execute. Maybe I'm doing this right and the secondary execute is occurring further down in the php templates, I'm not sure. I just need to know if what I have done so far looks good, ty all.


回答1:


Replace this condition:

RewriteCond %{REQUEST_URI} !^/myadmin/

With these 2:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

Putting it all together:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^myadmin/(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /myadmin/index.php?vlng=$1&tpl=$4 [QSA,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(([a-z]{2})(-[a-z]{2})?)(/([a-z0-9-\./]*))?$ /index.php?lng=$1&tpl=$4 [QSA,L,NC]



回答2:


Ok I found the problem, it was partly my fault for assuming the Clear Cache extension in my Chrome browser only worked on a single tab at a time and a default setting in that extension that automatically reload all tabs after the extension was activated.

Like most developers I have between 5-10 tabs open in my browser at any given moment while developing to access testing areas of my site, development tools and technical resources. When I work on code I generally do all my cache cleaning by hand personally so that I know it is really done, in this case I chose to add a new extension to my Chrome browser called 'Clear Cache' to help me take care of this a little faster because I was making a lot of small changes which I needed to make clean tests of each time. What I didn't realise was that when you activate the extension it also cleans the cache of all your other tabs as well. I also didn't notice the default setting that is set to automatically reload them all after the cleaning is performed. Because it also doesn't show you that it is reloading all these other tabs visually I had no idea it was reloading a tab that I had open to the main page of the site I was working on that was responsible for loading cookies.

To summarize while I worked on my site, in one tab, testing changes to my admin area I kept seeing cookie updates that could only come from the public side of my site and it kept making me think there was something still wrong with my htaccess changes. I realized, after two days of troubleshooting, the handy dandy extension I'd added to help make things easier was actually reloading all the tabs in my browser without letting me know and all those other calls were responsible for the continual return of cookies that could only be generated by the public side of my site which was confusing the hell out of me. I'll be going to go back to clearing my cache by hand again after this.



来源:https://stackoverflow.com/questions/18089392/htaccess-rewritecond-request-uri-detection-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!