[英]Help with mod_rewrite rule for dynamic url
嗯.. mod_rewrite让我感到愚蠢。 我只是还没有动脑筋。 :/
我有这个网址:
http://example.com/a/name/
...我想在这里指出:
http://example.com/a/index.php?id=name
...其中name
是作为id
参数传递给index.php
name
。
我尝试过的任何结果都会导致404或500。.:(
如果希望尾部的斜杠为可选,则必须排除将请求重写到的文件。 否则,您将有一个很好的无限递归。
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/a/index\.php$
RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]
在这里,任何以/a/…
开头但不是/a/index.php
都将重写为/a/index.php
。
但是,如果必须在结尾加上斜杠,则无需排除目标文件:
RewriteEngine on
RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]
首先开始:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]
如果一个重写教程对您不起作用,请尝试另一个 。
编辑:根据Gumbo的建议排除了index.php
也许有些类似的东西
RewriteEngine on
RewriteBase /a/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]
会成功的
我建议您看一下以下URL:
http://www.dracos.co.uk/code/apache-rewrite-problem/
提出的解决方案可以使用,但URL中有一些警告说明,主要涉及? 和#放在网址本身中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.