首页 文章

htaccess重写不起作用

提问于
浏览
0

我希望你能帮忙 . 我正在尝试写一个htaccess文件,执行以下操作 .

1)重定向到www . 地址

2)从url中删除.php

3)如果文件不存在则使用filechecker.php?page = filename


1我能做到

RewriteCond% ^ example.com $

RewriteRule( . *)http://www.example.com/ $ 1 [R = 301,L]


2)我能做到

RewriteCond% .php -f

RewriteRule [^/] $% .php [QSA,L]


3)我想

RewriteCond% .php!-f

RewriteRule ^([^/] *)$ filechecker.php?page = $ 1 [QSA,L]

会工作,但由于某种原因,它忽略了页面确实存在的事实 .


我希望你能帮助马克

1 回答

  • 0

    您的2解决方案将循环,但很简单,可以修复,在第2和第3部分的文件中沿着以下行添加一些内容:

    #If the Browser request contains a .php, instruct the browser to remove it.
    RewriteCond %{THE_REQUEST}      \.php      [NC]
    RewriteRule ^/?(.*)\.php$       http://%{HTTP_HOST}/$1         [R=301,NC,L]
    
    #If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention.
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-s
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !^\.php$  [NC]
    RewriteRule ([^/]+)$      -  [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php]
    
    #See if the file exists with a .php extention, if it does internally rewrite
    RewriteCond %{ENV:testScript} -s  [OR]
    RewriteCond %{ENV:testScript} -f
    RewriteRule .*              %{ENV:testURI} [L]
    
    #Else if a ENV:testDile is set, then pass the name to the php script
    RewriteCond %{ENV:testFile} !^$
    RewriteRule .*               /filechecker.php?page=%{ENV:testFile} [L]
    

    仅供参考:Apache mod_rewrite文档值得一读,如果您不清楚上述规则的作用,或者您想要更抽象的内容,请考虑以下post .

相关问题