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

后端 未结 1 673
耶瑟儿~
耶瑟儿~ 2021-01-28 06:54

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

相关标签:
1条回答
  • 2021-01-28 07:45

    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]
    
    0 讨论(0)
提交回复
热议问题