繁体   English   中英

如何在htaccess中重写SEO友好的URL markep

[英]how to rewrite SEO-friendly url markep in htaccess

我正在尝试将我的URL转换为SEO友好URL。 我对htaccess重写规则了解不多,URL为:

https://www.zesteve.com/search.php?loc=Guntur&q=manjunath-cake-shop

该网址应为

https://www.zesteve.com/Guntur/manjunath-cake-shop

我已经试过了:

RewriteEngine On  
RewriteCond $1 ^search 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?q=$1 [L,QSA]

我得到的结果是

https://www.zesteve.com/search?loc=Guntur&q=manjunath-cake-shop

编辑

我正在使用jQuery重定向

 window.location.href = 'search?loc=' + location[0] + '&q='+ search_term;

如果有任何身体寻找相同的解决方案。 这是我的答案

首先,它应该重定向并转换为SEO友好URL

# Redirect to SEO Friendly Url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:search\.php)?\?loc=([^\s]+)&q=([^\s]+)? [NC]
RewriteRule ^ /%1/%2? [R=301]

现在设置URL

RewriteCond $1 ^ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ search.php?loc=$1&q=$2 [L,QSA]

它解决了我的问题。

https://www.zesteve.com/search.php?loc=Guntur&q=manjunath-cake-shop

上面的URL现在是

https://www.zesteve.com/Guntur/manjunath-cake-shop

我不想在URL中显示search.php页面

RewriteRule ^([^/]*)/([^/]*)$ /search.php?loc=$1&q=$2 [L]

在您的.htaccess文件中使用此命令:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /search.php?loc=$1&q=$2 [L]

它将为您提供以下URL: https://www.zesteve.com/Guntur/manjunath-cake-shop : https://www.zesteve.com/Guntur/manjunath-cake-shop 只需确保在测试前清除缓存即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM