.htaccess 5个变量的问题

时间:2014-01-17 17:05:51

标签: php apache .htaccess mod-rewrite

以下是我的.htaccess文件。

AddDefaultCharset utf-8
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

DirectoryIndex index.php
RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)?$ index\.php?page=$1&s=$2 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/?$ index\.php?page=$1 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})?$ index\.php?page=$1 [L]

ErrorDocument 404 /404

当我做的时候

ehco "<pre>";
print_r($_REQUEST);
echo "</pre>";

有3个参数,它显示了我

Array
(
    [page] => test
    [s] => 1
    [o] => 9
    [p] => 
    [q] => 
)

但如果超过3个参数,则表示未找到。 .htaccess适用于以下网址 的为myhost / mysite的/页/ S / O 但不是 的为myhost /页/ S / O / P

2 个答案:

答案 0 :(得分:1)

您的代码发生了一些变化:

AddDefaultCharset utf-8
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]

DirectoryIndex index.php
RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3&p=$4 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)/([^/]+)?$ index\.php?page=$1&s=$2&o=$3 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/([^/]+)?$ index\.php?page=$1&s=$2 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})/?$ index\.php?page=$1 [L]

RewriteRule ^([a-zA-Z0-9_-]{3,20})?$ index\.php?page=$1 [L]

ErrorDocument 404 /404

答案 1 :(得分:0)

你正确的完整.htaccess可以是这样的:

AddDefaultCharset utf-8
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

DirectoryIndex index.php

RewriteRule ^(\w{3,20})/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index\.php?page=$1&s=$2&o=$3&p=$4&q=$5 [QSA,L]

RewriteRule ^(\w{3,20})/([^/]+)/([^/]+)/([^/]+)/?$ index\.php?page=$1&s=$2&o=$3&p=$4 [QSA,L]

RewriteRule ^(\w{3,20})/([^/]+)/([^/]+)/?$ index\.php?page=$1&s=$2&o=$3 [QSA,L]

RewriteRule ^(\w{3,20})/([^/]+)/?$ index\.php?page=$1&s=$2 [QSA,L]

RewriteRule ^(\w{3,20})/?$ index\.php?page=$1 [QSA,L]

ErrorDocument 404 /404