使用.htaccess在多个条件下进行URL重写/重定向/限制 [英] URL Rewriting/Redirecting/Restricting on Multiple Conditions using .htaccess
问题描述
服务器版本:Apache/2.4.18(Win32)OpenSSL/1.0.2e PHP/7.0.8
Server Version : Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.8
我知道这个问题已经问了无数次了,虽然我有零件解决方案,但是我没有一个解决方案,所有运动的零件都能正常工作.
I realize this question has been asked numerous time, and while I have solution in parts, I do not have a solution in which all moving parts work properly..
我有4种url
首先...(索引文件重定向)
First... (index file redirect)
http://localhost/myproject/index.php -我想... http://localhost/myproject
第二....(此解决方案是系统范围内必需的-适用于所有.php文件) http://localhost/myproject/views/forgotpassword.php -我想要 http://localhost/myproject/views/forgotpass
Second.... (this solution is required system wide - for all .php files) http://localhost/myproject/views/forgotpassword.php - which I want as http://localhost/myproject/views/forgotpassword
第三...(此解决方案是系统范围内必需的-适用于所有/views/文件) http://localhost/myproject/views/forgotpassword.php -我想要 http://localhost/myproject/forgotpassword
Third... (this solution is required system wide - for all /views/ files) http://localhost/myproject/views/forgotpassword.php - which I want as http://localhost/myproject/forgotpassword
第四...(此解决方案仅适用于两个文件)
Fourth... (this solution is for two files only)
http://localhost/myproject/views/propertydetail.php?property_id = 4 -我想要 http://localhost/myproject/property/4
我想限制对所有以前的名称和路径的访问(我认为这是不可能的,因为这样做会使代码无法正常工作-即这样做之后什么也不起作用),所以我认为将使用重定向,但如果可能的话,将进行限制是必需的...
I would like to restrict access to all previous names and path (which I think is not possible because doing that makes the code not work - i.e nothing works after doing that) so I think redirecting will be used, but if possible restriction is required...
以下是我的合并的.htaccess文件
Following is my consolidated .htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
## hide views folder
RewriteRule ^$ views/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ views/$1
## remove special characters and clean the urls
RewriteRule ^property/([0-9]+) views/propertydetail.php?property_id=$1
RewriteRule ^user/([0-9]+) views/viewprofile.php?profile_id=$1
只有第一部分(php隐藏和重定向有效)其余视图文件夹删除和字符清理无效.我喜欢解决方案的第一部分,因为这样我就不必修改我的代码,一切都保持不变...
with this only the first part (php hiding and redirecting is working) rest views folder removal and character cleaning is not working. I like the first part of the solution because then I don't have to modify my code, everything remains the same...
推荐答案
通过一些重构,您的规则需要采用不同的顺序,例如:
With some some refactoring your rules need to be in a different order like this:
ErrorDocument 404 default
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /myproject/
## hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L,NE]
## remove special characters and clean the urls
RewriteRule ^property/(\d+)/?$ views/propertydetail.php?property_id=$1 [L,QSA,NC]
RewriteRule ^user/(\d+)/?$ views/viewprofile.php?profile_id=$1 [L,QSA,NC]
## hide views folder
RewriteRule ^$ views/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!view/)(.*)$ views/$1 [L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/? $1.php [L]
这篇关于使用.htaccess在多个条件下进行URL重写/重定向/限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!