开发者

why doesn't this rewrite the url properly?

RewriteCond %{REQUEST_URI} !^/?new.php?url
RewriteRule ^/?(.+)\.?(.+)$ new.php?url=$0 [L]

开发者_Go百科its supposed to take any URL

mysite.com/someurl

and convert it to

new.php?url=someurl

however it keeps going to just new.php


You need to escape the second question mark in the first line so it matches a literal question mark:

RewriteCond %{REQUEST_URI} !^/?new.php\?url

Also you are not using the parenthesized groups on the second line. $0 is okay, or you may want $1 instead. If you use $0 you could simplify it a bunch:

RewriteRule ^.*$ new.php?url=$0 [L]

Or on the other hand if you're breaking apart the URL for a reason I would suggest some fixup. You're not matching the file name and extension exactly right. A little more complex regex like this would probably do you better:

RewriteRule ^/?(.*?)(?:\.([^.]*))?$ new.php?path=$1&extension=$2 [L]

Explanation:

  1. (.*?) matches the directory and file name. The ? means match non-greedily, so stop as soon as the next part matches. The parentheses cause it to be captured and stored in $1.

  2. (?:\.([^.]*))? matches the file extension. ?: turns says to not capture the outer set of parentheses, so the dot is not captured in $2. ([^.]*) matches the extension and ensures that it does not contain a dot. The final ? makes the file extension part optional, just cause not all URLs have file extensions. Thus there will only be a $2 if there is a file extension.


The first back-reference is $1 not $0 AFAIK. If that doesn't do it try specifying [QSA] as well, though I doubt that's it.

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


The first back reference should be $1 instead of $0.

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

It also depends on what other lines of code you have in the file. It's also possible to mess up rewrites if you have another code that conflicts with it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜