首页 文章

网址重写不起作用的htaccess

提问于
浏览
0

这是我的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]

显然我想要这个网址:www.example.com/username被翻译成http://www.example.com/index.php?id=82&user=username

但这不起作用..(此代码导致htaccess根本无法正常工作并且找不到Page页面错误 .

如果我改变] $ for a]?代码确实有效,但不是我想要的:

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 .

现在,如果我删除了RewriteRule . * /index.php行,htaccess再也不起作用了,导致找不到页面错误...

我花了几天时间来搞清楚这一点,但我绝对无能为力......

所以,我只想让www.example.com/username重定向到http://www.example.com/index.php?id=82&user=username

1 回答

  • 1

    这里有几个问题 .

    • 在.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]
    

相关问题