I don't know why but when use the follow RewriteRule in my .htaccess file I am getting a Internal Server Error 500. I have mod_rewrite enabled I've been looking for a solution all day.
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
RewriteRule ^(.*)/?$ users\/$1\/index.php
Most likely the 500 error is due to a redirect loop.
You rewrite ANY request to users/$1/index.php
including that very URL itself so you end up with a rewrite loop.
Try changing your rewrite rules to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/users
RewriteRule ^(.*)/?$ users/$1/index.php
By checking that the URI doesn't begin with /users
you can avoid the rewrite loop.
Apache's error log (often named errors.log
) probably provides more info about why it doesn't work. Most likely the regular expression is invalid for some reason, I'm going to test it myself and then edit this post.
You have an internal rewrite loop. The rewrite engine takes a URI and puts it through the engine and gets a resulting URI. If they are the same, the rewrite engine stops, otherwise, it takes the URI it got back and puts it back through the engine . This means your /users/something/index.php
is matching ^(.*)/?$
again and getting turned into: /users/users/something/index.php/index.php
, then /users/users/users/something/index.php/index.phpusers/something/index.php/index.php
etc. etc.
So you need to add a RewriteCond
to prevent URI's from getting re-rewritten when they already start with users/
:
# Enable Rewriting
RewriteEngine on
# Don't rewrite if URI starts with "users"
RewriteCond %{REQUEST_URI} !users/
# Rewrite user URLs
RewriteRule ^(.*)/?$ users\/$1\/index.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.