帮助动态网址的mod_rewrite规则

时间:2023-02-10 11:22:39

Ugh.. mod_rewrite makes me feel stupid. I just haven't wrapped my brain around it yet. :/

呃.. mod_rewrite让我感到愚蠢。我还没有把我的大脑包裹起来。 :/

I have this url:

我有这个网址:

http://example.com/a/name/

...that I want to point here:

......我想在此指出:

http://example.com/a/index.php?id=name

...where name is what is getting passed to index.php as the id argument.

...其中name是作为id参数传递给index.php的内容。

Anything I've tried results in either a 404 or a 500.. :(

我试过的任何东西都会产生404或500 ...... :(

4 个解决方案

#1


To start you off:

为了让你开始:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]

If one rewrite tutorial doesn't work for you, try another.

如果一个重写教程不适合你,请尝试另一个。

Edit: excluded index.php as per Gumbo's suggestion

编辑:根据Gumbo的建议排除index.php

#2


If you want the trailing slash to be optional, you have to exclude the file you are rewriting the request to. Otherwise you will have a nice infinite recursion.

如果希望尾部斜杠是可选的,则必须排除要重写请求的文件。否则你将有一个很好的无限递归。

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]

Here any request that starts with /a/… but it not /a/index.php is rewritten to /a/index.php.

这里的任何请求都以/ a / ...开头,但不是/a/index.php会被重写为/a/index.php。

But if the trailing slash is mandatory, there is no need to exclude the destination file:

但如果尾部斜杠是必需的,则无需排除目标文件:

RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]

#3


Maybe something along the lines of

也许有些事情


RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]

would do the trick.

会做的伎俩。

#4


I suggest you take a look at this URL:

我建议你看看这个网址:

http://www.dracos.co.uk/code/apache-rewrite-problem/

The presented solutions will work, but there are some caveats explained in the URL, mainly regarding ? and # in the URLs themselves.

提出的解决方案将起作用,但URL中有一些注意事项,主要是关于?和URL中的#。

#1


To start you off:

为了让你开始:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]

If one rewrite tutorial doesn't work for you, try another.

如果一个重写教程不适合你,请尝试另一个。

Edit: excluded index.php as per Gumbo's suggestion

编辑:根据Gumbo的建议排除index.php

#2


If you want the trailing slash to be optional, you have to exclude the file you are rewriting the request to. Otherwise you will have a nice infinite recursion.

如果希望尾部斜杠是可选的,则必须排除要重写请求的文件。否则你将有一个很好的无限递归。

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]

Here any request that starts with /a/… but it not /a/index.php is rewritten to /a/index.php.

这里的任何请求都以/ a / ...开头,但不是/a/index.php会被重写为/a/index.php。

But if the trailing slash is mandatory, there is no need to exclude the destination file:

但如果尾部斜杠是必需的,则无需排除目标文件:

RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]

#3


Maybe something along the lines of

也许有些事情


RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]

would do the trick.

会做的伎俩。

#4


I suggest you take a look at this URL:

我建议你看看这个网址:

http://www.dracos.co.uk/code/apache-rewrite-problem/

The presented solutions will work, but there are some caveats explained in the URL, mainly regarding ? and # in the URLs themselves.

提出的解决方案将起作用,但URL中有一些注意事项,主要是关于?和URL中的#。