How to name file and directory the same with .htaccess rewrite rules?

后端 未结 2 855
感动是毒
感动是毒 2021-01-27 13:16

I am trying to work on my \"pretty-urls\" and I\'ll be the first to admit I really don\'t know what I\'m doing. I\'m trying to wrap my head around it though. So what I am trying

2条回答
  •  一向
    一向 (楼主)
    2021-01-27 14:15

    Have it this way:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^dire1/file/([0-9]+) dir1/file.php?id=$1 [NC,QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^dir2/example/([0-9]+) dir2/example.php?id=$1 [NC,QSA,L]
    
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    Last rule will serve /file.php when URI is example.com/file/

    Note that if you serve example.com/file (no trailing slash) then Apache's mod_dir module will add a trailing slash in the end to make it example.com/file/ and then server /file.php.

    Turning off this trailing slash behavior is considered a security risk and it may expose your internal directory structure to your visitors.

提交回复
热议问题