doumeng3080 2014-01-05 18:39
浏览 144
已采纳

htaccess重定向与GET请求

Following is my htaccess:

Options -Multiviews -Indexes
RewriteEngine on
RewriteBase /

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

## hide .php extension snippet

# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?seo=([^&\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?seo=$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
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]

Above codes are working fine except for following two scenario:

  1. .php extension is not getting removed for pages under 'Forum' directory. Their is no GET request in these pages. If I remove the condition for Forum above then gives 404 error Forum/.php not found.

  2. Also, .php extension is not getting removed for pages where GET request variable is other than 'seo'.

  3. URL with 'seo' as GET variable are converting into SEO friendly but GET request with other variables like 'id' or 'rg=&vp=&da=' are not working. I have added additional rules (as for seo) for the same but still no impact.

Kindly advise.

  • 写回答

1条回答 默认 最新

  • duanhuan8983 2014-01-06 17:30
    关注

    Have your .htaccess like this:

    Options -Multiviews -Indexes
    RewriteEngine on
    RewriteBase /
    
    ## hide .php extension snippet
    
    # To externally redirect /dir/foo.php?id=123 to /dir/foo
    RewriteCond %{THE_REQUEST} \s(.+?)\.php\?seo=([^&\s]+)(?:&(\S+))? [NC] 
    RewriteRule ^ %1/%2?%3 [R,L]
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} \s(.+?)\.php(\S*) [NC]
    RewriteRule ^ %1%2 [R,L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ $1.php [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?seo=$2 [L,QSA] 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献