【问题标题】:Apache Rewrite Rules for SEO URLSEO URL 的 Apache 重写规则
【发布时间】:2015-12-12 21:55:19
【问题描述】:

我正在尝试使用 Apache mod_rewrite 来获得 SEO URL,我有以下内容:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1

RewriteRule ^([^/]*)\.php$ /index.php?a=$1 [L]
RewriteRule ^([^/]*)/([^/]*)\.php$ /index.php?a=$1&id=$2 [L]

重定向到 www 和 https 的第一部分工作正常,但我需要实现以下不起作用的 URL 重写:

  1. https://www.domain.com/index.php?a=explorehttps://www.domain.com/explore/
  2. https://www.domain.com/index.php?a=page&b=abouthttps://www.domain.com/about/
  3. https://www.domain.com/index.php?a=track&id=9https://www.domain.com/track/idno
  4. https://www.domain.com/index.php?a=explore&filter=newtaghttps://www.domain.com/explore/newtag/

【问题讨论】:

  • 如果URL是https://www.domain.com/foo/bar那你想怎么改写?
  • 抱歉忘记了现在修复的链接之间的to。

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

以下规则应该有效:

RewriteCond %{THE_REQUEST} ^GET\ /index\.php?a=(explore)(?:&filter=([^\s&]+))? [NC]
RewriteRule ^index\.php$ /%1/%2? [R=301,L,NC]

RewriteCond %{THE_REQUEST} ^GET\ /index\.php?a=(track)&id=(\d+) [NC]
RewriteRule ^index\.php$ /%1/%2/? [R=301,L,NC]

RewriteCond %{THE_REQUEST} ^GET\ /index\.php?a=page&b=about [NC]
RewriteRule ^index\.php$ /about/? [R=301,L,NC]

RewriteRule ^(explore)/(.*)/?$ /index.php?a=$1&filter=$2 [L]
RewriteRule ^(track)/(\d+)/?$ /index.php?a=$1&id=$2 [L]
RewriteRule ^(about)/?$ /index.php?a=page&b=$1 [L]

【讨论】:

  • 谢谢,这对我有用,但是我遇到了脚本的另一个问题,因为默认情况下,当我浏览它使用原始 URL 但访问重写的网站时,脚本不使用 URL 的重写版本urls 直接工作正常。我正在使用 phpsound.com/demo 。我是否需要以某种方式添加代码并告诉脚本使用重写的网址而不是原始网址?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多