I have a .htaccess file in which I'm attempting to redirect requests to
/something/
to
/something
...after which a second rule should run which turns
/something
...into
/index.php?foo=something
Here's my .htaccess:
RewriteEngine on
#rule 1 - disallow trailing slash
RewriteRule ^([a-z\-]+)\/$ $1
#rule 2 - main redirect
RewriteCond %{REQUEST_URI} !(inc/) [NC]
RewriteRule ^[a-z\-]+$ index.php?foo=$0
Both rules take effect correctly, but the redirect in the first rule goes to a 404 and I can't see why. The correct flow should be:
Any idea what I'm doing wrong?
You need an R
flag to redirect:
DirectorySlash Off
RewriteEngine on
#rule 1 - disallow trailing slash
RewriteRule ^([a-z\-]+)\/$ $1 [L,R]
#rule 2 - main redirect
RewriteCond %{REQUEST_URI} !(inc/) [NC]
RewriteRule ^[a-z\-]+$ index.php?foo=$0
Also, you may need to turn DirectorySlash
off, because mod_dir will automatically append trailing slashes to diretories. If you dont turn that off, it might cause a redirect loop.
Additionally, you may also need a RewriteBase
directive if these rules are in an htaccess file that isn't in your document root.
RewriteBase /
for document root, but in your question you have URLs that look like http://localhost/mysite/something/
and if the rules are in /mysite/
you need:
RewriteBase /mysite/
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.