的.htaccess友好的URL [英] .htaccess friendly URl

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

问题描述

任何人都可以请帮我做一些URL重写?

我有: (举例)

  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的文件:

 的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^([^ /] +)/?$?页= $ 1 [L]

的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^([^ /] +)/([^ /] +)/ $ 1页= $&放大器;???主题= $ 2
 

但它不工作得很好,因为我可以这样写:

  • 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(NO OK,我想404,但是我们看到namepage)
  • www.example.com/gallery/topic(好吧,我想,我们看到nametopic)
  • www.example.com/whatever/whatever2/whatever3(好吧,我想404)

非常感谢给任何人。

解决方案

  ###所有的重定向

#为www.example.com/index.php?page=homepage&paging=1
的RewriteCond%{THE_REQUEST} \页=([^&安培] +)?&安培;寻呼=([0-9] +)
重写规则^ / 1%/ 2%? [L,R = 301]

#为www.example.com/index.php?page=gallery&topic=nametopic
?的RewriteCond%{THE_REQUEST} \页=([^&安培;] +)及主题=([^&安培; \] +)
重写规则^ / 1%/ 2%? [L,R = 301]

#为www.example.com/index.php?page=namepage
?的RewriteCond%{THE_REQUEST} \页=([^&放大器; \] +)($ | \)
重写规则^ /%1? [L,R = 301]

#为www.example.com/namepage/
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(。*)/ $ / $ 1 [L,R = 301]

###所有的重写回

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

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

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

Can anybody please help me with some URL rewriting?

I had: (EXAMPLES)

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

I would like to have:

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

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 (page or whatever)
  • www.example.com/?page=namepage (page or whatever)
  • www.example.com/namepage/
  • www.example.com/namepage (THIS I WANT - no others)

And second problem is:

  • www.example.com/namepage (OK, i want, we see namepage)
  • www.example.com/namepage/whatever (NO OK, i want 404, but we see namepage)
  • www.example.com/gallery/topic (OK, i want, we see nametopic)
  • www.example.com/whatever/whatever2/whatever3 (OK, i want 404)

VERY THANKS TO ANYBODY.

解决方案

### 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆