[英]Help with mod_rewrite rule for dynamic url
Ugh.. mod_rewrite makes me feel stupid. 嗯.. mod_rewrite让我感到愚蠢。 I just haven't wrapped my brain around it yet. 我只是还没有动脑筋。 :/ :/
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
name
。
Anything I've tried results in either a 404 or a 500.. :( 我尝试过的任何结果都会导致404或500。.:(
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]
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
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. 会成功的
I suggest you take a look at this URL: 我建议您看一下以下URL:
http://www.dracos.co.uk/code/apache-rewrite-problem/ 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 ? 提出的解决方案可以使用,但URL中有一些警告说明,主要涉及? and # in the URLs themselves. 和#放在网址本身中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.