0

我尝试并尝试使用此处发布的示例,但我没有设法使我的 htaccess 正常运行。

情况是这样的:我的链接看起来像这样

domain.com/sport/football/index.php?lang_id=1&page_id=500 (主页) domain.com/sport/football/index.php?lang_id=1&page_id=505 (球员) domain.com/sport/football/index.php?lang_id=1&page_id=510 (教练) ...

我想将它们重命名为

domain.com/sport/football/

domain.com/sport/football/players/

domain.com/sport/football/coaches/

等等......并且所有非指定的page_id都重定向到主页。

非常感谢所有帮助。

4

2 回答 2

0

在文档根目录的 htaccess 文件中,添加:

RewriteEngine On
RewriteRule ^sport/football/$ /sport/football/index.php?lang_id=1&page_id=500 [L,QSA]
RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L,QSA]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L,QSA]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=500($|&)
RewriteRule ^ /sport/football/? [L,R=301]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=505($|&)
RewriteRule ^ /sport/football/players/? [L,R=301]

RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /sport/football/index\.php
RewriteCond %{QUERY_STRING} ^(.*)page_id=510($|&)
RewriteRule ^ /sport/football/coaches/? [L,R=301]
于 2013-11-09T19:42:58.903 回答
0

你可以使用RewriteMap Directive它。您必须定义从名称到 ID 的映射

players 505
coaches 510

并告诉 Apache 地图

RewriteMap football txt:/path/to/footballmap.txt

RewriteMap必须在主配置文件或指令VirtualHost中。

现在你可以使用这张地图了

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/(.*)/?$ /sport/football/index.php?lang_id=1&page_id=${footballmap:$1|500} [L]

如果没有找到密钥,将500使用默认(主页)。如果你有很多映射,你也可以使用 hashfile 代替。

更新:

当您无权访问服务器或虚拟主机配置文件时,您只能拥有一个固定的RewriteRule“映射”

RewriteRule ^sport/football/players/?$ /sport/football/index.php?lang_id=1&page_id=505 [L]
RewriteRule ^sport/football/coaches/?$ /sport/football/index.php?lang_id=1&page_id=510 [L]
# maybe other similar rules ...
# this is a catch everything else and must come last
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sport/football/ /sport/football/index.php?lang_id=1&page_id=500 [L]
于 2013-11-09T19:53:11.037 回答