I'm working on a project where I'm trying to do URL rewriting in the root of my website. The current .htaccess
is as follows
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L] RewriteRule ^screenshot-of-the-week/screenshot/([^/]+) /screenshot-of-the-week/screenshot/index.php?id=
My website has rewrite enabled. I have checked the code through htaccess tester and it shows the correct values.
However, when I visit screenshot-of-the-week/screenshot/123
for example, I get the 404 page. When I visit screenshot-of-the-week/screenshot/
it works fine.
Also, when manually going to the "real" URL, it works as expected: screenshot-of-the-week/screenshot/index.php?id=123
I can't seem to figure out what the problem is and have tried moving .htaccess
to a different folder and of course editing the path, but with no success.
Can anyone provide other troubleshooting tips? How can I check what the redirect is actually doing behind the scenes?
The "problem" with this rule is that it also matches the rewritten URL (specifically,
[^/]
matchesindex.php
), so it will be rewritten again Write time to/screenshot-of-the-week/screenshot/index.php?id=index.php
(id=123
parameter is overridden). So, I expect your script to generate a 404 response (not Apache)?To resolve this issue, you should make the regex more specific and/or use the
END
flag (Apache 2.4) to prevent the rewrite engine from doing an extra loop (and a second rewrite).In your example, you are passing a numeric value - so if you only expect a numeric value, only numbers will be matched.
For example:
Please note that I also added the trailing
$
to the regex, otherwise your site could be abused as anything can be appended to the URL and be the same The resources will be served. (Appending/
to your original rule will produce the same response.)(I assume your
.htaccess
file is in the document root.)The MWL tester performs only one pass through the file. This is not how a real Apache server works. Therefore, the MWL tester cannot detect rewrite/redirect loops.
When you visit
screenshot-of-the-week/screenshot/
, the rule is not processed (regex does not match). mod_dir provides aDirectoryIndex
document (index.php
), but noid
parameter.