I have modified .htaccess to remove trailing .html from my pages. htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.mydomain.co.uk} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://www.mydomain.co.uk/$1 [R=301,L]
I also added the following at the top of the .htaccess to set up a custom 404 page and stop indexing:
ErrorDocument 404 /notfound.html
Options -Indexes
The rewrite rule works perfectly. So does the anti-indexing. However the 404 page gives me this error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#mydomain.co.uk and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
If I remove the rewriterule then the 404 page works fine. I don't know a lot about this kind of stuff and was wondering what to do to be able to get both aspects to work correctly? Can anyone help please?
(PS: I read this answer and think it might apply to me - but didn't understand it at all.)
Just wanted to let you know I found a solution for this:
ErrorDocument 404 /notfound.html
Options -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.htm\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.htm$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.html [L]
Works great!
Related
I want to show a 503 error for a page on my website. (Using htacess) So far I've tried the following but it seems to be not working.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /league.php [R=503,L]
Is this correct? Or is there anyway to give the absolute URL path? Thanks.
Could you please try following, tested and written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/webmail/?$ [NC]
RewriteRule ^(.*)$ league.php [R=503,L]
OR using errordocument option.
ErrorDocument 503 /league.php
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/webmail/?$ [NC]
RewriteRule .* - [R=503,L]
I'm attempting to rewrite my URL's and some are working but another (which is an extra directory level deep) produces a 500 internal server error. I'm not sure why this is happening and for various reasons am unable to turn on logging.
My .htaccess file looks like this:
Options +FollowSymLinks
RewriteEngine On
# Rewrite account/order.php
RewriteRule ^account/order/([a-z0-9]+)/.+$ /account/order.php?order_id=$1 [L]
# Rewrite advertiser.php
RewriteRule ^advertiser/([a-z0-9]+)/.+$ /advertiser.php?advertiser_id=$1 [L]
# Remove .php from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The URL for the rewrite rule for advertiser.php might look like this: http://domain.com/advertiser/1234
This works just fine.
However, my rewrite rule for account/order.php doesn't. It produces a 500 error. The URL might look like this: http://domain.com/account/order/1234
The only difference that I can see is that the rewrite rule has another directory in it that the working one does not. What am I missing that might cause this error?
Thanks!
Not sure why you have extra .+ in the end of patterns. Try this code:
ErrorDocument 404 default
Options +FollowSymLinks
RewriteEngine On
# Rewrite account/order.php
RewriteRule ^account/order/([a-z0-9]+)/?$ account/order.php?order_id=$1 [L,QSA,NC]
# Rewrite advertiser.php
RewriteRule ^advertiser/([a-z0-9]+)/?$ advertiser.php?advertiser_id=$1 [L,QSA,NC]
# Remove .php from URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
i am using this code in the .htaccess-File to rewrite for example /home to /home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
but how can i do this and at the same time when the server gets a request for /home.php
rewrite it so the file will not be found. I only want to allow access with /home and
not with /home.php but still use the file with it's extension.
Using this .htaccess works in general:
ErrorDocument 404 /404_error.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} \.php[\s?/] [NC]
RewriteRule \.php$ - [NC,R=404,L]
But when a request that should throw 404 uses for example /whatever the custom 404 error page gets loaded. But with /whatever.php i get this:
The requested URL /test/whatever.php was not found on this server.
Additionally, a 500 Internal Server Error error was encountered while trying to use an
ErrorDocument to handle the request.
Any idea how to solve this 500 error?
P.S.: Same thing without php-extension "ErrorDocument 404 /404_error"
You can use this rule to block .php requests:
RewriteCond %{THE_REQUEST} \.php[\s?/] [NC]
RewriteRule \.php$ - [NC,R=404,L]
I'm trying to make my own MVC PHP framework using a tutorial that i found over the net .
but i got a problem understanding the rewrite_mod in the htaccess files. here is the first part :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
1) As written in the tutorial those rules will redirect all the request to the public folder , so the first question is why we have two rules ? what means the first and the second. one
2) the second part is another htaccess file in the public folder containing :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
the second part will rewrite the url to index.php?url=$1 this part is clear but this part is little bit difficult for me
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
i learn about it and it tell that the request should not be a file or directory but the index.php is a file (in public directory) .
anothe question please why when we remove the last .htaccess file (in public directory) we
got this error :
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at admin#127.0.0.1 to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
and when we have only a htacces containting only this part ,it works great ?
<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
Thanks so much .
You can combine and fix the root .htaccess:
RewriteEngine on
RewriteRule !^public/ public%{REQUEST_URI} [L]
This means forward to /public/<uri> if REQUEST_URI does not start with /public
Some explanation now.
DOCUMENT_ROOT/.htaccess:
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
1st rule is internally forwarding to public/ with request URI is empty i.e. http://site.com/
2nd rule is internally forwarding even URI to public/<URI>
DOCUMENT_ROOT/public/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
Here inside public direcotry:
RewriteCond %{REQUEST_FILENAME} !-f means if request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-d means if request is not for a valid directory
RewriteRule ^(.*)$ index.php?url=$1 means forward request to index.php?url=<uri>
Reference: Apache mod_rewrite Introduction
I use this rewrite code to rewrite all requests to .php file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]
So when I enter index the index.php will load or if I enter contact the contact.php will load.
But when I enter a name which does not exists I will get Internal Server Error, for example I entered this name test or fdgdfg/ewrtt/ddfghdfg, after this I will get this error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster#mydomain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
I have ErrorDocument 404 error.php too in my .htaccess.
this is my complete .htaccess file :
ErrorDocument 404 index.php
RewriteEngine on
RewriteBase /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]
You need to check that the php file actually exists, otherwise the 2 conditions that you have will always fail and the .php extension keeps getting appended, e.g. you'd end up with a URI looking like:
/shop/foo.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php
until the internal recursion limit is reached and you get a 500 error. Try adding another condition to check if the file exists:
ErrorDocument 404 index.php
RewriteEngine on
RewriteBase /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [L]
Following solved my problem -
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]