的.htaccess友好的URL友好、htaccess、URL

2023-09-02 00:27:50 作者:劳娘人字拖也能走天下ㄨ

任何人都可以请帮我做一些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
 
IIS URL Rewrite Module防盗链规则配置方法

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

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]