How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

后端 未结 2 1418
轻奢々
轻奢々 2021-01-27 04:56

For the past 3 days I have been playing around with Apache\'s mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.

相关标签:
2条回答
  • 2021-01-27 05:16

    This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
    RewriteRule ^ %1%2 [R=302,L]
    

    Change R=302 to R=301 once you're satisfied that it is working fine for you.

    0 讨论(0)
  • 2021-01-27 05:20

    This rule:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
    RewriteRule ^ /%1 [R=301,L]
    

    Needs to look like this:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\  [NC]
    RewriteRule ^ /%1 [R=301,L]
    

    Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

    0 讨论(0)
提交回复
热议问题