htaccess的的RewriteCond不工作 [英] htaccess rewritecond not working

查看:258
本文介绍了htaccess的的RewriteCond不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能有所帮助。我试图写一个htaccess文件做到以下几点。

I hope you can help. I'm trying to write a htaccess file do the following.

1)重定向到www。地址

1) redirect to www. address

2)从URL中移除的.php

2) remove .php from url

3)如果文件不存在,则使用filechecker.php?页=文件名

3) If the file doesn't exist then use filechecker.php?page=filename

1,我可以做

的RewriteCond%{HTTP_HOST} ^ $ example.com

RewriteCond %{HTTP_HOST} ^example.com$

重写规则(。*) http://www.example.com/ $ 1 [R = 301, L]

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

2)我可以做

的RewriteCond%{} SCRIPT_FILENAME -f的.php

RewriteCond %{SCRIPT_FILENAME}.php -f

重写规则[^ /] $%{REQUEST_URI}的.php [QSA,L]

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

3)我想

的RewriteCond%{} REQUEST_FILENAME .PHP!-f

RewriteCond %{REQUEST_FILENAME}.php !-f

重写规则^([^ /] *)$ filechecker.php?页面= $ 1 [QSA,L]

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

会的工作,但由于某种原因被忽略的页面不实际存在的事实。

would work, but for some reason it is ignoring the fact that the page does actually exist.

我希望你能帮助
马克

I hope you can help Mark

推荐答案

您2将循环解决方案,但很简单修复,坚持沿着您的文件中以下行的东西部分2和3:

Your solution for 2 will loop, but simple enough to fix, stick something along the following lines in your file for part 2 and 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]

FYI:Apache的的mod_rewrite 文档值得一读,如果你以什么上面的规则做不清楚,或者如果你想要的东西多一点抽象考虑以下的帖子

FYI: The Apache mod_rewrite documentation is worth a read, if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post.

这篇关于htaccess的的RewriteCond不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆