简体   繁体   English

htaccess重定向使用mod_rewrite为seo友好的网址

[英]htaccess redirect using mod_rewrite for seo friendly urls

I'm trying to trim down my urls using htaccess having my index.php act as the key with the following code: 我正在尝试使用htaccess减少我的URL,我的index.php充当以下代码的密钥:

  RewriteEngine on
  RewriteRule ^([^/]+)$ index.php?city=$1 [NC]

To use this with my members area, do i just add the following to work on mysite.com/members/index.php?city=$1 要在我的会员区使用此功能,我是否只需添加以下内容即可使用mysite.com/members/index.php?city=$1

      RewriteRule ^members/([^/]+)$ index.php?city=$1 [NC]

in the event of a city having multiple words, ie: new york city, whats the best way to approach this? 如果一个城市有多个单词,即:纽约市,最好的办法是什么? defining it in the .htaccess (not sure if thats possible) to change new york city -> new-york-city or actually any space to a dash. 在.htaccess中定义它(不确定是否可能)改变纽约市 - >纽约市或实际上任何空间到破折号。 Or, do it in php with a function to replace ' ' -> '-' for the db queries that use $_GET? 或者,在php中使用函数来替换'' - >' - '用于使用$ _GET的数据库查询?

I would replace spaces with dashes in PHP and then redirect to the new version of the URL to get easily readable addresses. 我会用PHP中的破折号替换空格,然后重定向到新版本的URL以获得易于阅读的地址。 (Space is encoded as %20 which is not exactly easily readable) (空格编码为%20,不易读取)

That way you would still be able to enter the address domain.com/new york city but would be redirected to domain.com/new-york-city instead of domain.com/new%20york%20city . 这样你仍然可以进入地址domain.com/new york city但是会被重定向到domain.com/new-york-city而不是domain.com/new%20york%20city

The replace in .htaccess is possible with something like this: .htaccess中的替换可能是这样的:

RewriteRule ^([^\s]*)\s(.*) $1-$2 [N]

(Based on answer to Search and replace in apache htaccess a RewriteRule ) (根据搜索的答案并在apache中替换htaccess一个RewriteRule

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

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