麻烦匹配模式与.htaccess文件中mod_rewrite的查询字符串匹配

时间:2014-10-22 07:56:45

标签: php apache .htaccess mod-rewrite

我的项目中有4个网址,我正在尝试使用mod_rewrite

进行翻译
hall/description_banquethall.php?hall_id=1 > hall/description_banquethall/1

hall/venues.php?city=hyderabad&city_value=1&lacality=malakpet&locality_value=82 > hall/venues/hyderabad/1/malakpet/82

hall/hall_booking.php?hall_id=1&booking_date=2014-10-23&session=Morning > hall/hall_booking/1/2014-10-23/Morning

hall/booking_message.php?booking_id=10 > hall/booking_message/10

到目前为止,我在.htaccess中有以下内容,适用于

hall/booking.php?hall_id=5&booking_date=2014-10-23&session=Morning

hall/description_banquethall.php?hall_id=5

但不适用于上面列出的两个网址:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall/

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

## hide .php extension snippet

RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?city=([^&\s]+)&city_value=([^&\s]+)&locality=([^&\s]+)&locality_value=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?city=$2&city_value=$3&locality=$4&locality_value=$5 [L,QSA]

RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?booking_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?booking_id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?booking_id=$2 [L,QSA]

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d

1 个答案:

答案 0 :(得分:0)

为什么不在你的.htaccess

中使用它
<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)/?$ index.php?page=$1 [L]

</IfModule>

然后在index.php中返回相应的页面:

# SELECT WHICH PAGE TO DISPLAY
function getPage($showPage) {
    $modules = array(#never name the slugs as same as the php pages
    '/sign-in/i' => 'register.php',
    '/signout/i' => 'logout.php',
    '/staff/i' => 'admin.php',
    '/products/i' => 'blank.php',
    );
    foreach ($modules as $slug=>$phpmodule) {
        if(preg_match($slug, $showPage)) { 
            return $phpmodule;
        }
    } 
    return 'blank.php';#in case module not found display 404 page
}

if (isset($_GET['page'])) {
require_once (getPage($_GET['page'])); #include the appropriate module file
}