与htaccess的URL重写不工作
这是我的htaccess代码:与htaccess的URL重写不工作
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)$ http://www.example.com/index.php?id=82&user=$1 [L,R=301]
显然,我希望这个网址: www.example.com/username 被翻译成http://www.example.com/index.php?id=82&user=username
这不起作用然而..(在htaccess的这段代码的结果不是在所有的工作,并得到一个找不到网页错误
如果我改变] + $对于] +代码的工作,但没有像我想:?
RewriteEngine On
RewriteRule ^/typo3$ - [L] RewriteRule ^/typo3/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* /index.php
RewriteRule ^/([a-zA-Z0-9_-]+)? http://www.example.com/index.php?id=82&user=$1 [L,R=301]
URL被重写/重定向到http://www.example.com/index.php?id=82&user=index的结果...正是这样,用user = index。现在
,如果我删除重写规则。*的index.php线,htaccess的再不能在工作中都下去了,导致找不到网页错误...
我花了几天在搞清楚了这一点,但我绝对没有线索天..
所以,我只是想www.example.com/username重定向到http://www.example.com/index.php?id=82&user=username
这里有几个问题。
- 在.htaccess文件中,模式匹配"against the filesystem path, after removing the prefix"。这意味着,您将没有前导斜杠,如
/typo3
或^/([a-zA-Z0-9_-]+)?
- 模式
([a-zA-Z0-9_-]+)?
也匹配一个空请求,因为尾随?
。我想,这不是你想要的。 - 规则按顺序处理,除非你做一个重定向
[R]
或添加[L]
标志。这就是为什么第一个请求被改写为index.php
,然后在接下来的规则index
是公认的用户,并再次改写为.../index.php?id=82&user=index
- 这导致了下一个问题的模式
.*
和([a-zA-Z0-9_-]+)
的原因。.*
承认所有请求,包括每一个用户。因此无法区分用户和其他任何请求。
要重写的用户名,你可以尝试
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+) http://www.example.com/index.php?id=82&user=$1 [L]
这意味着,如果该请求不符合现有的文件!-f
或目录!-d
,则假定它是一个用户名和重写index.php?...
。
如果你不想重定向,离开了主机名
RewriteRule ^([a-zA-Z0-9_-]+) /index.php?id=82&user=$1 [L]
感谢您的意见Olaf。你会如何建议我写我的代码,以达到我需要的东西?谢谢你的帮助。 – 2014-10-31 08:59:55
如何区分用户名和其他有效请求? – 2014-10-31 09:03:52
这是一个很好的问题,我希望我知道答案 事情是,我需要这个功能:如果常规页面或文件夹已经存在,那么不要重写请求,只显示该文件夹或文件(例如:www.example.com/admin ..但是,如果它不存在,请将其重写为用户URL(/index.php?id=82&user=username) – 2014-10-31 09:07:28
我想通过简单的匹配(唯一)的用户名来获取ID ...我看到没有其他办法。但RewriteRules仍然不能在我的服务器上工作。真奇怪。 – 2014-11-06 11:17:50
**通过简单地匹配(唯一)用户名来获得ID。**匹配如何完成? – anubhava 2014-11-06 11:27:43
通过进行SQL查询比较 – 2014-11-06 13:48:12