您好我使用了许多不同的htaccess代码,但我无法重写工作。 我想要这个网址 https://domain.com/category.php?cat=firm 看起来像这个网址 https://domain.com/category/firm
这是我最近的尝试
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www\.seoboost\.no$
RewriteRule ^/?$ "https\:\/\/seoboost\.no\/" [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com\/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index\.php$ / [R=301]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301]
我试图在我的htaccess中删除所有内容并且只有这段代码
RewriteEngine on
RewriteRule ^([^/]*)\.html$ /category.php?cat=$1 [L]
但它仍然无效,是我的服务器还是我做错了什么?
答案 0 :(得分:0)
尝试用
替换你的.htaccessRewriteEngine On
RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
会在内部将yoursite.com/category/12
重定向到yoursite.com/category.php?cat=12
,用户永远不会看到"丑陋的"网址。然后在您的category.php文件中,您可以通过$category_id = $_GET['cat']
一个简单的锚标记如下所示:
<a href="https://yoursite.com/category/12">Item 12</a>
答案 1 :(得分:0)
从Justin Lurman's answer here拉出,以下内容应该有效:
RewriteEngine On
# redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
RewriteCond %{THE_REQUEST} \s/category\.php\?cat=(.+)\s [NC]
RewriteRule ^ /category/%1? [R=301,L]
# internally rewrite "/category/..." to "/category.php?cat=..."
RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
正如Justin在对类似问题的回答中所指出的,您需要确认Apache配置中启用/允许htaccess文件并且mod_rewrite
已启用。
此外,我相信你知道,但是如果你不是,在实施这个新的htaccess重定向规则之后,你应该改变你网页上的所有链接以使用干净/友好的URL。通过这样做,您的用户和搜索引擎爬虫都不会访问“丑陋”的URL,除非他们直接访问它们(通过书签或保存的链接)。