多个RewriteRules& RewriteConds干扰

时间:2015-01-29 19:01:21

标签: php apache .htaccess mod-rewrite

所以我在.htaccess中有以下代码:

RewriteEngine On

# Remove .php from URL and add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]

# Rewriting data from URL
RewriteRule ^minecraft/(.*)/$ /minecraft/$1/ [NC,L]
RewriteRule ^game/(.*)/$ /game/$1/ [NC,L]
RewriteRule ^tool/(.*)/$ /tool/$1/ [NC,L]

# Redirection if file or folder doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ ./router.php?r=/$1 [QSA,R,L]

我想实现以下目标:

  1. 用户可以访问“domain.com/contact/”,它会在根目录中显示文件“contact.php”的内容。 (注意URL中的尾部斜杠)
  2. 用户可以访问“domain.com/random-id/”,该请求将被路由到“router.php?r = random-id”。
  3. 用户可以访问“domain.com/minecraft/random-name/”,该请求将被路由到“minecraft / random-name /”文件夹。 (里面有index.php等)
  4. 我似乎无法弄清楚,任何帮助都表示赞赏!

1 个答案:

答案 0 :(得分:1)

这样做:

DirectoryIndex index.php
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]

# Remove .php from URL and add trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

# Redirection if file or folder doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ router.php?r=/$1 [QSA,L]