本文介绍了.htaccess 友好的 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我重写一些 URL 吗?

Can anybody please help me with some URL rewriting?

我有:(示例)

www.example.com/index.php?page=namepage
www.example.com/index.php?page=gallery&topic=nametopic
www.example.com/index.php?page=homepage&paging=1

我想要:

www.example.com/namepage
www.example.com/gallery/nametopic
www.example.com/homepage/1

我的 htaccess 文件中有:

I have in my htaccess file:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?/?$ ?page=$1&topic=$2

但效果不是很好,因为我可以写:

But it doesnt work very well, because i can write:

  • www.example.com/index.php?page=namepage(页面或其他)
  • www.example.com/?page=namepage(页面或其他)
  • www.example.com/namepage/
  • www.example.com/namepage(这是我想要的 - 没有其他)

第二个问题是:

  • www.example.com/namepage(好吧,我想要,我们看到了 namepage)
  • www.example.com/namepage/whatever(不行,我想要 404,但我们看到了名称页)
  • www.example.com/gallery/topic(好吧,我想要,我们看到 nametopic)
  • www.example.com/whatever/whatever2/whatever3(好吧,我想要 404)

非常感谢任何人.

推荐答案

### all your redirects

# for www.example.com/index.php?page=homepage&paging=1
RewriteCond %{THE_REQUEST} ?page=([^&]+)&paging=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=gallery&topic=nametopic
RewriteCond %{THE_REQUEST} ?page=([^&]+)&topic=([^& ]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=namepage
RewriteCond %{THE_REQUEST} ?page=([^& ]+)($| )
RewriteRule ^ /%1? [L,R=301]

# for www.example.com/namepage/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [L,R=301]

### all your rewrites back

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&paging=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&topic=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

这篇关于.htaccess 友好的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:03