简体   繁体   English

htaccess重写规则重定向到文件与否

[英]htaccess rewrite rule to redirect to file or not

I have been trying to write a set of htaccess rules that will do the following: 我一直在尝试编写一组htaccess规则,它们将执行以下操作:

  • With the url: mydomain.com/blog/something 使用网址:mydomain.com/blog/something

    • If a file called blog.php exists in 如果存在名为blog.php的文件
      the web directory(the directory with index.php in) then redirect to web目录(index.php中的目录)然后重定向到
      blog.php?url=something blog.php的?URL =东西

    • If blog.php does not exist, redirect to index.php?url=blog/something 如果blog.php不存在,请重定向到index.php?url = blog / something

    • EDIT: mydomain.com/blog/something is an example, it could be any url string and the htaccess should search the web directory for a file corresponding to the first part of the url string, which in this case is "blog" 编辑:mydomain.com/blog/something是一个例子,它可以是任何url字符串,htaccess应该在web目录中搜索对应于url字符串的第一部分的文件,在这种情况下是“blog”

In both cases I can then use $_GET['url'] to get parameters from the url to intiate whatever actions in my php script. 在这两种情况下,我都可以使用$ _GET ['url']从url获取参数,以便在我的php脚本中启动任何操作。

I currently have: 我目前有:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]

</IfModule>

But unfortunately this is not working, I'm not sure why, I'm proficient enough with php but my htaccess skills are pretty limited. 但不幸的是,这不起作用,我不知道为什么,我对PHP很熟练,但我的htaccess技能非常有限。 I don't know if the [L] option is the one I should be using or if I should be using more options. 我不知道[L]选项是否应该使用,或者我是否应该使用更多选项。 Currently whatever the url string is, $_GET['url'] returns "index.php". 目前无论url字符串是什么,$ _GET ['url']都会返回“index.php”。

If any more information is required just ask. 如果需要更多信息,请询问。

I hope someone can help Regards Luke 我希望有人可以帮助问候卢克

I hope that the following directives will work for you. 我希望以下指令对您有用。

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Apache mod_rewrite Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html Apache mod_rewrite参考: http//httpd.apache.org/docs/2.2/mod/mod_rewrite.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM