htaccess的重写URI到如果没有文件get /文件夹 [英] htaccess to rewrite uri into get if not file/folder

查看:83
本文介绍了htaccess的重写URI到如果没有文件get /文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的htaccess规则重写的URI到GET变量,如果它不是现有的文件或文件夹。 例如:

  www.example.com/page1.php  - >转到page1.php中
www.example.com/page2.html  - >转到page2.html
www.example.com/folder  - > GOTO /文件夹
www.example.com/some_string_value  - >改写为/default.php?value=some_string_value
 

解决方案

这确实是一个非常普遍的要求。有可能是1000年的这个要求对这个网站的问题。你需要使用的RewriteCond REQUEST_FILENAME 寻找不存在的文件夹,然后在内部重写以获得变量。从本质上讲,如果它是一个 404(不存在URI)将被路由到您的default.php文件。这就是pretty的URL的完成。您可以在根把这个在你的.htaccess文件。

  RewriteEngine叙述上
#prevent使用直接default.php文件,并重定向到友好URI
的RewriteCond%{} THE_REQUEST [AZ] {3,9} \ /default\.php\?value =([^&放大器; \] +)
重写规则^ /%1? [R = 301,L]
#REDIRECT不存在(404)文件夹来获得变量
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-d
重写规则^([^ /] +)/ $ /default.php?value=$1 [L]
 

I need htaccess rule that rewrites an uri into get variable if it is not for an existing file or folder. Example:

www.example.com/page1.php -> goto page1.php
www.example.com/page2.html -> goto page2.html
www.example.com/folder -> goto /folder
www.example.com/some_string_value -> rewrite as /default.php?value=some_string_value

解决方案

That's really a very common request. There are probably 1000's of questions about this asked on this site. You need to use the RewriteCond and REQUEST_FILENAME to look for non existent folder and then internally rewrite to get variable. Essentially if it's a 404 (non existent URI) it will be routed to your default.php file. That's how pretty URL's are done. You can put this in your .htaccess file in the root.

RewriteEngine On
#prevent the use of the default.php file directly and redirect to friendly URI
RewriteCond %{THE_REQUEST} [A-Z]{3,9}\ /default\.php\?value=([^&\ ]+)
RewriteRule ^ /%1? [R=301,L]
#redirect non existent (404) folder to get variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /default.php?value=$1 [L]

这篇关于htaccess的重写URI到如果没有文件get /文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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