2014-10-22 102 views
0

我有4个网址,在我的项目,我想使用mod_rewrite翻译麻烦.htaccess文件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 

但不能用于两个URL上面列出:

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 

回答

0

为什么不u使用这个在你的.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 
} 
+0

是这样有用吗?如果是,请投票或提供一些反馈意见。 – 2014-10-22 16:55:15