apache - 复杂的 .htaccess 重写

标签 apache .htaccess mod-rewrite url-rewriting friendly-url

您可以让我知道 htaccess 代码来重定向那里的网址吗:

  1. https://www.example.com/forum/news/state-news/archivehttps://www.example.com/forum/news/state-news?archive=yes

  2. https://www.example.com/forum/news/state-news/city-news/archivehttps://www.example.com/forum/news/state-news/city-news?archive=yes

  3. https://www.example.com/forum/news/state-news/city-news/area-news/archivehttps://www.example.com/forum/news/state-news/city-news/area-news/?archive=yes

我尝试过的 htaccess 文件是:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css.php$ core/css.php [NC,L]
RewriteRule ^install/ core/install/ [NC,L]

RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?routestring=$1 [L,QSA]
</IfModule>

最佳答案

通过以下方式获取您的 htaccess 规则文件。我已经具体介绍了您在问题中显示的 3 个 URL。请确保您使用这些规则,并确保在测试您的网址之前清除浏览器缓存。

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css\.php$ core/css.php [NC,L]

####I have commented below rule that doesn't make sense, since NO right part in it only condition is mentioned.
####RewriteRule ^install/core/install/ [NC,L]

###New rules added by me to handle 3 different URLs shown by OP in question.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(forum/news/state-news(?:(?:/city-news(?:area-news))))/(archive)/?$ $1?$2=yes [NC,QSA,L]

RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?routestring=$1 [L,QSA]

</IfModule>

关于apache - 复杂的 .htaccess 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67115892/

相关文章:

python - PAM认证问题

windows - 如何在 Apache 和 Win7 下使用 node.js 在端口 80 上创建服务器?

html - 使用 .htaccess 进行 RewriteRule 但每个文件都指向一个文件

linux - Apache - 将流量从别名重定向到端口

Apache mod_rewrite 子域到子文件夹(通过内部重定向)

php - 从 WAMP 迁移到 LAMP : File write errors from PHP

apache - 为什么这个 FilesMatch 没有正确匹配?

php - 将 HTML 站点移动到 PHP

php - 是否可以使用 htaccess 或类似的方法在每个 html 文件上运行 php 代码?

Apache 使用不同的转发代理重写请求