Reputation: 3570
I have the following .htaccess file in my root directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Which is removing .php extensions in the URL (eg. www.example.com/about or www.example.com/about/, yet still loading the page (www.example.com/about.php). The only change I've needed to make to my HTML so far is to include a ../
infront of every href
or src
.
Now I have come to use the following code at the end of a PHP login script:
header("Location: ../dashboard/");
Which should take me to www.example.com/dashboard.php but instead throws the following error:
The requested URL /example.com/dashboard.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The file is definitely uploaded. Is there any issue with my .htaccess which is causing the header()
to behave incorrectly?
EDIT
If I change the line to this:
header("Location: ../dashboard.php");
then it works, but I really don't want the file extension in the URL.
Upvotes: 0
Views: 1967
Reputation: 3570
I found my problem. It was a simple user mistake.
The directory I was working from wasn't truly the root directory. It was a parked domain within the same account hosting. so I was actually in root/example.com/dashboard.php
whereas the .htaccess file was in root/.htaccess
. Making a copy of the .htaccess file in to root/example.com/
fixed my problem.
Upvotes: 0
Reputation: 1357
Try this in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
</IfModule>
And then for your header redirect, simply do this:
header("location: ../dashboard")
Without the end trailing slash (/).
Upvotes: 0
Reputation: 785581
Change your rules to check for existence of .php
file before rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
Upvotes: 1