All requests to my site should be rewritten to index.php?page=blah
, where blah is the page that's requested (except for css, js, jp(e)g, gif and png files).
This is how my .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
The .htaccess is in this directory: localhost:8080/example/
, so when I go to localhost:8080/example/abc
, it is (internally) rewritten to localhost:8080/example/index.php?page=abc
.
However when I go to localhost:8080/example/res
, I get redirected to localhost:8080/example/res/?page=res
. I found out that this only happens to directories; when I go to localhost:8080/example/core
(also a folder on my file system), I get redirected to localhost:8080/example/core/?page=core
while it should be internally rewritten to localhost:8080/example/index.php?page=core
and the url visible to the user should stay localhost:8080/example/core/
EDIT:
Thanks to @w3dk, who solved the problem stated above. But I found another problem, which may be related to the problem above:
When I go to:
localhost:8080/example/index/a
, it's internally rewritten to localhost:8080/example/index.php?page=index.php/a
, while it should be rewritten to localhost:8080/example/index.php?page=index/a
.
I found out that this happens when index
is a file, cause when I go to localhost:8080/example/exampleFile/abc
, it's redirected to localhost:8080/example/index.php?page=exampleFile.php/abc
, which shouldn't be the case.
The 2 files in my directory are:
Apache seems to ignore the php file extension, cause this also works for exampleFile.txt
Save the file and type the URL yoursite.com/foobar/ . If the reditect works and the URL gets redireted to the homepage of example.com then it's clear that your htaccess is working and being read by your Apache server. If it still doesn't work then the problem might be that your hosting provider has not enabled it.
htaccess file will only override the mod_rewrite directives in httpd. conf if the directives in httpd. conf are also in a <Directory> container.
This is probably happening because of a conflict with mod_dir. The default behaviour (DirectorySlash On
) is for mod_dir to automatically "fix" the URL when you request a physical directory without a trailing slash. It does this with an external 301 redirect, before your rule is processed. Your rule then fires, which modifies the target URL, a Location
header gets returned to the client and the browser redirects.
This won't happen if you include the trailing slash on the original request. eg. localhost:8080/example/core/
. mod_dir then does not need to "fix" the URL and issue a redirect. Although this may not be desirable for you?
Since you are wanting to internally rewrite all directories then the simple fix is to disable this behaviour in .htaccess
:
DirectorySlash Off
You will need to clear your browser cache before testing, as the earlier 301s by mod_dir will have been cached locally.
Reference (note the security warning):
https://httpd.apache.org/docs/current/mod/mod_dir.html#directoryslash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With