RewriteRule creating 500 Internal Server Error

后端 未结 4 2189
盖世英雄少女心
盖世英雄少女心 2020-12-07 01:03

I have the following in my .htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^directory/(.*)$ directory/index.php?id=$1

4条回答
  •  借酒劲吻你
    2020-12-07 01:55

    Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^directory/(.*)$

    Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.

    Change your code to this:

    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^directory/(.*)$ /directory/index.php?id=$1 [L,QSA,NC]
    

    Above code has an extra RewriteCond %{REQUEST_FILENAME} !-f that will make sure to disallow subsequent execution of RewriteRule after first time since /directory/index.php will be a valid file.

提交回复
热议问题