开发者

How to make .htaccess RewriteRule without affecting index.php rules

I'm using GET requests in my project and I want to rewrite URLs in such a simple way: if I visit mydomain.com/test it will request mydoma开发者_如何学Pythonin.com/get.php?txt=test on my serverside.

Here is mine .htaccess RewriteRule, it works OK, but problem that it's also affects my index.php, so if I go to main page it starts: mydomain.com/get.php?txt=test request and not opening mydomain.com/index.php because of my RewriteRule.

Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*)$ get.php?url=$1 [L]

I don't want make any extra sublevels in the path like mydomain.com/1/test, it should be on the next level of root.

Any help appreciated. Thank you!


The most common way is to add conditions to only rewrite non-existing resources (files & folders):

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) get.php?url=$1 [L]

Another possible approach (which will work fine for your original request) is to ignore such rule when /index.php is requested:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule (.*) get.php?url=$1 [L]

Approach #1 is highly recommended and is pretty much universal. But .. maybe you serve all resources (images/styles/javascripts/etc) via get.php as well (I do not know what your application does and how it works) hence I have provided another approach.


Add

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

before rewrite rule.


Would this be okay?

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*)$ get.php?url=$1 [L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜