代码之家  ›  专栏  ›  技术社区  ›  AJFMEDIA

冲突的htaccess规则.php文件扩展名仍然可以访问

  •  0
  • AJFMEDIA  · 技术社区  · 16 年前

    我一直在尝试重新编写我的网址。前4条规则很重要,但它们与这条线冲突:(我认为)。

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
    

    这将停止像www.example.com/page.php那样访问URL,并重定向到www.example.com/page。/

    在添加了htaccess中可以看到的前4个规则之后,上面的条件似乎不起作用,我能够访问这样的URL:。(

    lovelakedistrict.com/lake-district-cottages.php/cottages/5/

    显然我应该这样

    lovelakedistrict.com/lake-district-cottages/cottages/5/

    但是这仍然有效:)

    lovelakedistrict.com/lake-district-cottages.php--> lovelakedistrict.com/lake-district-cottages/

    这是我的htaccess文件-有人能看到发生了什么吗?

    Options +FollowSymlinks
    Options +Indexes
    
    RewriteEngine on
    #these 4 rules stop being able to access pages like eg
    #/lake-district-cottages/?cottages=5/
    #/lake-district-cottages/?cottages/5/     --> all direct to  /lake-district-cottages/cottages/5/
    
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
    RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
    RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]
    RewriteCond %{REQUEST_URI} !^/result
    RewriteCond %{REQUEST_URI} !^/shop-result
    
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
    RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
    RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]
    RewriteCond %{REQUEST_URI} !^/result
    RewriteCond %{REQUEST_URI} !^/shop-result
    
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
    RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
    RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]
    RewriteCond %{REQUEST_URI} !^/result
    RewriteCond %{REQUEST_URI} !^/shop-result
    
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
    RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
    RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]
    RewriteCond %{REQUEST_URI} !^/result
    RewriteCond %{REQUEST_URI} !^/shop-result
    
    # this stops you accessing pages with php extention eg
    #/lake-district-cottages.php --> directs to /lake-district-cottages/ 
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
    
    # this ignors the include folder and does not add trailing slash (so ajax file works)
    RewriteCond %1 !^include/
    RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
    
    #this removes php extention
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]
    
    #this removes php extention and adds trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/$ $1.php
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
    
    #these re-write urls  allowing  / to replace ? and =
    RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
    RewriteRule ^lake-district-hotels/hotels/([0-9]+) lake-district-hotels.php?hotels=$1
    RewriteRule ^lake-district-bed-and-breakfast/bed-and-breakfast/([0-9]+) lake-district-bed-and-breakfast.php?bed-and-breakfast=$1
    RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
    RewriteRule ^lake-district-cottages-shop/lake-district-book-shop/([0-9]+) lake-district-cottages-shop.php?lake-district-book-shop=$1
    RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
    RewriteRule ^result/([a-zA-Z0-9])/([0-9]+) result.php?$1=$2
    RewriteRule ^shop-result/([a-zA-Z0-9])/([0-9]+) shop-result.php?$1=$2
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Tim Stone    16 年前

    条件模式与包含 PATH_INFO 但是将它简化为类似这样的东西应该注意:

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
    RewriteCond %1 !^include/
    RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301,L]