.htaccess从url中删除index.php和其他人

时间:2013-08-30 01:22:36

标签: php .htaccess

我遇到一个问题,即google使用错误的网址索引某些网页。

他们正在编制索引的网址是:

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重写URL。例如http://www.mydomain.com/index.php?a=page&b=about,因此生成后会生成类似于此RewriteRule ^([^/]*)/([^/]*)$ /index.php?a=$1&b=$2 [L]

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

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

  4. 谢谢!

  5. 编辑2:因为冲突而无法正常工作。更准确的解决方案涉及此主题.htaccess friendly URl

2 个答案:

答案 0 :(得分: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]

etc.etc。

答案 1 :(得分:1)

当我第一次开始时,我对.htaccess并不太精明。

查看此网站以及.htaccess生成器 - 它帮助我退出ALOT,我认为它将解决您的问题。

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

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

相关问题