douxing5199 2014-10-22 07:56
浏览 48

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

I have 4 urls in my project that I am trying to translate using 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

So far, I have the following in .htaccess which works for

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

and

hall/description_banquethall.php?hall_id=5

but not for the two URLs listed above:

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条回答 默认 最新

  • dongping8572 2014-10-22 10:30
    关注

    Why don't u use this in your .htaccess

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

    Then in your index.php you return the appropriate page:

    # 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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line