ht-access 如何重定向到论坛主题网址?

时间:2021-01-02 13:05:12

标签: .htaccess url redirect mod-rewrite url-rewriting

我想知道如何使用 ht-access 重定向我的页面,如下所示 .com/?page=forums&topic=topic-name 要更改为 .com/forums/topic-name?

下面的规则将所有页面名称重定向到 .com/index.php?page=forums,例如 .com/forums 或 .com/polls。

RewriteRule ^(.+)$ index.php?page=$1 [L]

我希望 www.treyarder.com/home 重定向到 www.treyarder.com/index.php?page=home 以及 www.treyarder.com/forums/topic-name 重定向到 www.treyarder.com/index.php?page=forums&topic=topic-name

我的话题在 www.treyarder.com/index.php?page=forums&topic=topic-name

我的ht-access文件如下图

RewriteEngine on   
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^(.+)$ index.php?page=$1 [L]

RewriteCond %{HTTP_HOST} \.com [NC]
RewriteCond %{REQUEST_URI}  !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1&topic=%2 [L]

ErrorDocument 404 /index.php?pages=404
ErrorDocument 500 /index.php?pages=500

下面这行似乎有问题

RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]

有人知道是什么吗?好像我可以修复那条线那么这应该可以解决我的问题。

2 个答案:

答案 0 :(得分:0)

当您通过前端控制器模式处理所有非文件、非目录请求时,您必须从 .htaccess 中删除 ErrorDocument 行。

您可以在站点根目录 .htaccess 中尝试这些规则:

RewriteEngine On
   
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&topic=$2 [L,QSA]

## COMMENT OUT THESE 2 LINES
# ErrorDocument 404 /index.php?pages=404
# ErrorDocument 500 /index.php?pages=500

css/js/images 问题:这是由于您使用了这些资源的相关链接。尝试任何以下两种解决方法:

  1. 在您的 css、js、图像文件中使用绝对路径而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 /
  2. 开头
  3. 在页面 HTML 的 <head> 标记下方添加:<base href="/" /> 以便每个 相对 URL 都从该基本 URL 而非当前页面的 URL 解析。< /li>

答案 1 :(得分:0)

好吧,这是我们设法混合和匹配直到成功为止的最终结果。

RewriteEngine on  

RewriteCond %{HTTP_HOST} \.com [NC]
RewriteCond %{REQUEST_URI}  !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1&topic=%2 [L]
RewriteCond %{REQUEST_URI}  !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1 [L]
相关问题