【问题标题】:htacces redirect for SEO friendly URLshtaccess 重定向 SEO 友好的 URL
【发布时间】:2015-01-03 00:45:23
【问题描述】:

我有我的 htaccess 文件设置,以便页面删除扩展名。现在,我正在尝试将传输变量的页面转换为对 SEO 友好的 url...所以,例如...

http://www.example.com/art-gallery?page=2 ...实际上是“art-gallery.php?page=2”,会变成...http://www.example.com/art-gallery/page/2

或者……http://www.example.com/art-piece?id=3……会去……http://www.example.com/art-piece/id/3

...等等...

我的 htaccess 文件中有很多内容,但不确定如何执行上述操作(有很多关于从 www.example.com/index.php?page=2 到 www.example.com/page 的教程/2/ 但没有一个能完全满足我的需要)。理想情况下,我希望能够对所有类似页面执行此操作...

# enable the rewrite engine
RewriteEngine On
# Set your root directory
  RewriteBase /

# Force www:
  RewriteCond %{HTTP_HOST} ^example.com [NC]
  RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

# Remove the .php extension
  RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
  RewriteRule (.*)\.php$ $1 [R=301]

# Remove index and reference the directory
  RewriteRule (.*)/index$ $1/ [R=301]

# Remove trailing slash if not a directory
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} /$
  RewriteRule (.*)/ $1 [R=301]

# Forward request to html file, **but don't redirect (bot friendly)**
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteCond %{REQUEST_URI} !/$
  RewriteRule (.*) $1\.php [L]

# Disable Directory Browsing
  Options -Indexes

# Disable Hotlinking of Images 
# with forbidden or custom image option
  RewriteCond %{HTTP_REFERER} !^$
  RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
  RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
  RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L] 

# Protect htaccess File
  <files ~ "^.*\.([Hh][Tt][Aa])">
  order allow,deny
  deny from all
  satisfy all
  </files>

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    您可以使用变量QUERY_STRING 传递参数。 考虑以下规则:

    RewriteRule ^index.html index.php?%{QUERY_STRING}&m=main&a=index
    

    这条规则会改变

    index.html?something=value
    

    进入

    index.php?something=value&m=main&a=index
    

    【讨论】:

    • 这个答案不会产生对 SEO 友好的网址。我需要一些需要... filename.html?something=value 并将其更改为 filename/something/value。
    【解决方案2】:

    您应该使用RewriteEngine

    您也可以单独使用 301 重定向或与 RewriteEngine 结合使用来重定向 SE。

    一般来说,虽然将 SE 重定向到与用户看到的页面不同的页面并不是一个好的做法,并且可能会导致您的页面排名下降。相反,请尝试将所有页面迁移到第二种 URL 格式,并考虑使用 301 重定向来帮助转换。

    通常:使用 301 重定向进行 SE 友好的页面更改。请参阅此SO 以获取更多参考。

    【讨论】:

    • 我被告知 301 重定向不适用于传递变量的 URL(即 art-piece?page=1),并被告知对这些类型的 URL 使用 RewriteRule。此外,我在 art-gallery?page=# 中有大量页面(其中 # 可以是任何数字)。所以,这将是很多重定向。当您说“将所有页面迁移到第二种 URL 格式...不确定您的意思,也不知道该怎么做...
    • 您应该能够使用正则表达式替换规则来修改您的 URL,从而为您提供 SEO 友好的 URL。尝试更多地使用 RewriteEngine。
    【解决方案3】:

    您可以在将请求转发到 html 文件规则之前插入此规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/])/([^/])/([^/])/?$ $1.php?$2=$3 [L,QSA]
    

    【讨论】:

    • 这些类型的 cmets 几乎没用,因为它无法说明您尝试了什么以及遇到了什么问题。
    【解决方案4】:

    这已经很老了,但为什么不做以下事情:

    RewriteEngine On
    RewriteRule ^([^?]*) index.php?route=$1 [L,QSA]
    

    然后在你的 index.php 中你可以这样处理它;

    if (isset($_GET['route'])) {
       $route = explode('/', $_GET['route']);
       if (iconv_strlen((end($parts)), 'UTF-8') == 0) {
           array_pop($parts);
       }
    }
    

    从这里开始,您的主要级别将由 $route[0] 处理,第二级 $route[1]

    例如;

     http://example.com/art-gallery/2
    

    $route[0] 等于“艺术画廊” $route[1] 等于 '2'

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2013-09-15
      • 2019-06-07
      • 1970-01-01
      • 2011-05-29
      • 2013-01-22
      相关资源
      最近更新 更多