php-.htaccess更改查询字符串的路径

我在example.com/中有文件

contact.php
recent.php
videos.php
watch.php

example.com/page/> / page /不是文件夹.但我想工作其他文件夹

contact.php(无分页)

example.com/page/contact

最近.php(分页>最近.php?page = 2)

example.com/page/recent
example.com/page/recent?page=2

watch.php(无分页> watch.php?title =戏剧名称)

example.com/page/watch/drama-name

videos.php(具有获取名称> videos.php?name = drama-name& page = 2的分页)

example.com/page/videos/drama-name
example.com/page/videos/drama-name?page=2

这是我完整的htaccess代码(example.com/.htaccess)

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$http://www.%{HTTP_HOST}/$1 [R=301,NE,L]

RewriteRule ^page/playlist$/playlist.php [L]
RewriteRule ^page/submit$/submit.php [L]
RewriteRule ^page/contact$/contact.php [L]
RewriteRule ^page/search$/search.php [L]
RewriteRule ^page/recent$/recent.php [L]

RewriteRule ^page/watch/([^/]+)? /watch.php?title=$1

RewriteRule ^page/videos/([^/]+)? /videos.php?name=$1

但无法在/ page / videos / drama-name中使用?page = 2如何解决?还有另一个问题吗?

解决方法:

您可以结合使用正则表达式,并使用QSA标志在.htaccess中缩短规则:

RewriteEngine on
RewriteBase /

# add www in host name
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*?)/?$http://www.%{HTTP_HOST}/$1 [R=301,NE,L]

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$/$1 [NE,R=301,L]

RewriteRule ^page/(submit|contact|search|recent|playlist)/?$$1.php [L,NC]

RewriteRule ^page/(videos|watch)/([^/]+)/?$$1.php?title=$2 [L,QSA,NC]
上一篇:通过HTML / PHP使用htaccess进行自定义登录


下一篇:拒绝访问目录中的所有文件,除非特定的php页面是referrer