【问题标题】:.htaccess remove index.php and others from url.htaccess 从 url 中删除 index.php 和其他
【发布时间】:2013-09-02 15:00:59
【问题描述】:

我有一个问题,谷歌索引了一些带有错误 url 的页面。

他们正在索引的网址是:

1. http://www.mydomain.com/index.php?a=profile&u=john
2. http://www.mydomain.com/index.php?a=page&b=about
3. http://www.mydomain.com/index.php?a=settings&b=avatar
4. http://www.mydomain.com/index.php?a=feed
5. http://www.mydomain.com/index.php?a=feed&filter=picture
6. http://www.mydomain.com/index.php?a=post&m=32
7. http://www.mydomain.com/index.php?lang=english
8. http://www.mydomain.com/index.php?a=messages&u=john&id=4
9. http://www.mydomain.com/index.php?a=feed&logout=1

我需要它重定向到:

1. http://www.mydomain.com/john or http://www.mydomain.com/profile/john
2. http://www.mydomain.com/about or http://www.mydomain.com/page/about
3. http://www.mydomain.com/settings/avatar
4. http://www.mydomain.com/feed
5. http://www.mydomain.com/feed/picture or http://www.mydomain.com/feed/filter/picture
6. http://www.mydomain.com/message/32
7. http://www.mydomain.com/lang/english
8. http://www.mydomain.com/messages/john
9. http://www.mydomain.com/logout or http://www.mydomain.com/feed/logout

.htaccess 不是我的强项,因此非常感谢任何帮助。

提前致谢。

编辑:

  1. 好的,我使用 Dan Trimper 和 Jon Lin 的两种方法让它工作了。首先,我使用 Dan Trimper 方法生成 mod rewrite url。比如http://www.mydomain.com/index.php?a=page&b=about,所以生成后会产生像RewriteRule ^([^/]*)/([^/]*)$ /index.php?a=$1&b=$2 [L]这样的url

  2. 其次,生成后我使用第二种方法将 url 重定向到 http://www.mydomain.com/page/about 通过使用 Jon Lin 方法:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php\?a=page&b=([^&\ ]+) 重写规则 ^ /page/%1? [L,R=301]

  3. 谢谢!

编辑 2:由于冲突,无法在上面工作。更准确的解决方案去这个话题.htaccess friendly URl

【问题讨论】:

标签: php .htaccess


【解决方案1】:

尝试将这些规则添加到文档根目录中的 htaccess 文件中:

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=profile&u=([^&\ ]+)
RewriteRule ^ /profile/%1? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=page&b=([^&\ ]+)
RewriteRule ^ /page/%1? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=settings&b=([^&\ ]+)
RewriteRule ^ /settings/%1? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=feed&logout=1
RewriteRule ^ /feed/logout? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=feed&filter=([^&\ ]+)
RewriteRule ^ /feed/%1? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=feed
RewriteRule ^ /feed/? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=feed&filter=([^&\ ]+)
RewriteRule ^ /feed/%1? [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=post&m=([^&\ ]+)
RewriteRule ^ /message/%1? [L,R=301]

等等。

【讨论】:

  • 谢谢。该网站转到正确的网址,但我得到 404 Not Found。我需要像 url rewrite 或用户友好的 url 之类的东西。 RewriteEngine 也打开了该模块。页面示例, 选项 +FollowSymLinks RewriteEngine On RewriteCond %{THE_REQUEST} ^[AZ]{3,9}\ /index\.php\?a=page&b=([^&\ ]+) RewriteRule ^ /page/%1? [R=301,L]
  • @napsters 如果http://www.mydomain.com/profile/john 不存在,为什么在世界你会将谷歌重定向到这些网址?
  • 我想修剪 URL,这有点像使用 htaccess 文件来处理漂亮的 URL。工作网址将是这样的 mydomain.com/index.php?a=profile&u=john 并希望像上面的 OP 一样更改它。
  • 好的,我用 Dan Trimper 和你的两种方法让它工作了。谢谢!
【解决方案2】:

当我刚开始的时候,我也不太熟悉 .htaccess。

查看此站点以及 .htaccess 生成器 - 它帮助了我很多,我认为它会解决您的问题。

http://www.generateit.net/mod-rewrite/

它会在您配置之后为您预先编写规则 - 然后复制/粘贴到您的 .htaccess 文件中。

【讨论】:

  • 谢谢。我会试试的。
猜你喜欢
  • 2011-05-20
  • 2017-03-15
  • 1970-01-01
  • 2021-10-02
  • 2015-01-20
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多