URL Rewriting/Redirecting/Restricting on Multiple Conditions using .htaccess

孤街醉人 提交于 2019-12-20 06:15:04

问题


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..

I have 4 types of url

First... (index file redirect)

http://localhost/myproject/index.php - which i want as... http://localhost/myproject

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

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 - which I want as 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...

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

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...


回答1:


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]


来源:https://stackoverflow.com/questions/42618308/url-rewriting-redirecting-restricting-on-multiple-conditions-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!