I was wondering why my htaccess would not allow for multiple rules? Here is the code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [QSA,L]
Basically I wanted to add a 404 rule. Here is the code:
ErrorDocument 404 http://xxxxxx/404.php
Well basically I can not add all of the together in one htaccess file. Here is the code:
ErrorDocument 404 http://xxxxxx/404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [QSA,L]
That code above will not work all together. Why is that?
It's not working because basically you are doing two times the same thing in the .htaccess:
ErrorDocument 404 http://xxxxxx/404.php
The ErrorDocument redirects to 404 in case the requested page is not found. The two RewriteConds RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d plus the RewriteRule will do exactly the same, they will redirect if the request file was not found - in your case they are redirecting to index.php.
So you can try either of the two following:
RewriteEngine on
ErrorDocument 404 /404.php
or
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.php [QSA,L]
Currently the URL is being treated as a string, to let it know it's a path you should add a slash at the beginning
try this and see if it works
ErrorDocument 404 /404.php
Related
I have the following rules in my .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/en-us/
Rewriterule ^(.*) /en-us/error-404/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) /error-404/ [L]
And it's working fine. When there are requests starts with /en-us/, I can see the content of /en-us/error-404/ page and in all other cases I see the content of /error-404/ page. However, the status code received from the server is 200 OK.
Is it possible to send 404 Not Found in both cases?
You can use if/elseif/else conditions to use different ErrorDocument directives: (available in Apache 2.4+)
<If "%{REQUEST_URI} =~ m#^/en-us/#">
ErrorDocument 404 /en-us/error-404/
</If>
<Else>
ErrorDocument 404 /error-404/
</Else>
So basically i created a .htaccess file which removes .html extension and redirects /file.html to /file but when i write /file.html/ it causes a 500 internal server error, How can I map/file.html/ to my custom error page ? or simply redirect that also to /file.html
Here is the code :
Options -Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 403 /custom404.html
ErrorDocument 500 /custom404.html
RewriteEngine on
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
With your shown samples, could you please try following(comments have been added in rules for explanation). Please make sure to clear your browser cache before testing your URLs.
Options -Indexes
ErrorDocument 404 /custom404.html
ErrorDocument 403 /custom404.html
ErrorDocument 500 /custom404.html
RewriteEngine on
##Added additional condition to make sure this runs from external request only.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/.*\.html\s [NC]
RewriteRule ^(.*)\.html/?$ /$1 [R=301,L,NC]
##Simply rewrite from non-existing pages to html files.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.*)/?$ $1.html [L]
In my .htaccess file, I have the settings for rewriting urls such as http://www.myownwebsite.com/about.php to http://www.myownwebsite.com/about/.
And I also have the settings for custom error 403 and 404 pages.
The issue I'm encountering is that, when I'm testing accessing a folder directly, for example, http://www.myownwebsite.com/css/, instead of seeing the custom 403 page, I'm seeing the custom 404 page. I guess that's because the settings of url rewriting is overwriting the setting of custom 403 page.
Here is the related code in my .htaccess file:
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
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]
What am I doing wrong?
Before adding .php to your URIs make sure corresponding .php file exists.
Better to keep redirect rule before other rules.
Have it like this:
Options -MultiViews -Indexes
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
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 %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
I have a htaccess that checks the IP and then redirects the user to the appropriate page. Right now I'm using ErrorDocument 404 /404.php but I want to have seperate 404 pages based on the IP. If I place that under the..
RewriteCond %{REMOTE_ADDR} x.x.x.x
rule it still goes to the same 404 page. How do I make the 404 page account for the IP like the rest of the pages do?
You cannot put conditions before ErrorDocument directive.
As a workaround you can use mod_rewrite to handle this situation for you:
RewriteEngine On
# handle 404 for IP=x.x.x.x
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} =x.x.x.x
RewriteRule . /ip1-404.php [L]
# handle 404 for IP=y.y.y.y
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REMOTE_ADDR} =y.y.y.y
RewriteRule . /ip2-404.php [L]
I'm trying to do four things with .htaccess
First thing is removing the php extension from all files
so
www.mywebsite.com/home is written www.mywebsite.com/home.php
I want to change to error page so if a request is made to www.mywebsite.com/adakadabra and there is such page like adakabra.php a custom 404.php is used unstead.
Thirdly I would like to have vain url such that users can have profile links like
www.mywebsite.com/Joe
I have a profile.php and I use a get request to get the username so the rewrite would be from
www.mywebsite.com/joe to www.mywebsite.com/profile.php?username=joe
Last but not least
I would want to have a pages like www.mywebsite.com/p/123453
to rewrite to
www.mywebsite.com/p.php?photo=123453
I have the page p.php on my webserver.
This can to it, with 3 sets of rules in /.htaccess:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([0-9]+)$ /p.php?photo=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Joe|Mao|Napoleon|Gandi)$ /profile.php?username=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ /$1.php [QSA,L]
Here how it works:
www.mywebsite.com/p/123453 => /p.php?photo=123453
www.mywebsite.com/Joe => /profile.php?username=Joe
www.mywebsite.com/home => /home.php,
www.mywebsite.com/houra/foo/bar/home => /houra/foo/bar/home.php