.htacces url_rewrite遇到困难

时间:2016-02-08 16:28:40

标签: php apache .htaccess mod-rewrite

我对我正在处理的小型网站.htaccess的配置存在问题。

我希望将所有页面重定向到index.php?page=REQUEST,该文件将在数据库中找到所请求页面的内容。

当我安装论坛时会出现问题,所以我希望这些论坛页面重定向到index.php?page=forum&params

Options +FollowSymlinks

RewriteEngine on 
RewriteCond %{REQUEST_URI} /(.*).html

RewriteRule ^(.*)forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L]

RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]

除了论坛部分外,Evetything工作正常。我如何更改.htacces

2 个答案:

答案 0 :(得分:1)

RewriteEngine on 

RewriteRule \.(jpg|png|gif|svg|css|js)$ - [L]

RewriteRule ^(.*)/forum/topic/(.*)?$ index\.php?page=forum&lang=$1&topic=$2 [L]
RewriteRule ^(.*)/forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L]

RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]

答案 1 :(得分:0)

问题似乎是您的.html匹配以.html结尾的请求。由于您的论坛网址不会以^(.*)forum结尾,因此永远不会满足后续RewriteRule的条件。

还有一些其他可能的问题:

    当你看起来可能只想www.url.com/en/

    时,
  • en会匹配category/(.*)

  • RewriteEngine on # only match forum URLs # e.g url.com/en/forum/category/12345 RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+ RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L] # match all URLs ending in .html # e.g. url.com/en/foo.html # and url.com/foo.html RewriteCond %{REQUEST_URI} ^/.+\.html$ # a bit complicated, this matches both # /apage.html # /folder/apage.html RewriteRule ^(?:/(.+))?/(.+)\.html$ index.php?lang=$1&page=$2 [L] 将匹配任何字符,包括正斜杠等。大概你只想让它匹配小数标识符。

  • 链接到重写配置未涵盖的内容,例如图像

我可能会重写你的配置看起来像这样(N.B。没有在Apache中测试过;只在regex调试器中测试过):

page

第二个RewriteRule应始终为lang提供值,但如果网址为/lang/page.html,则只提供lang的值。如果您的index.php文件可以接受空RewriteEngine on # don't actually rewrite, and stop processing rules RewriteRule \.(jpg|png|css|js)$ - [L] # only match forum URLs # e.g url.com/en/forum/category/12345 RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+ RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L] etc... 参数或提供默认值,那么这应该没问题。

或者,如果您不介意保留现有的正则表达式并且它只是想要绕过URL重写的图像,CSS等,您可以在开头添加一些规则来跳过它们,例如

<base href="/">