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]
Related
I have a problem when I try to add a new rule to my .htaccess. On my server I activated a rule which redirects http to https and it works fine but when I add a new rule to my .htaccess then https disappears.
My .htaccess
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
</IfModule>
I need to redirect every request to index.php (mvc) and it should work together with https.
I tried deactivating the redirection from the server and to write everything into my .htaccess like this:
Options -Indexes
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
But I get the error The page isn't redirecting properly. How to fix it? Thank you.
I've finally solved it with the help of my internet provider. This is the final code:
RewriteEngine on
RewriteBase /
# force https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^www.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
ErrorDocument 404 https://%{HTTP_HOST}/
ErrorDocument 403 https://%{HTTP_HOST}/
I have an error folder, which contains 404.php 403.php 500.php
I want to redirect user to the error page of any (if 404; redirect to 404.php, if 403; redirect to 403.php, if 500; redirect to 500.php) without changing the URL.
Here is my code in my htaccess
Options -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /error/404.php [L,NC]
RewriteRule ^ /error/403.php [L,NC]
RewriteRule ^ /error/500.php [L,NC]
Add to .htaccess file:
ErrorDocument 404 /error/404.php
ErrorDocument 403 /error/403.php
ErrorDocument 500 /error/500.php
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 seen many posts on here about both mod_rewrite and non-www. to www. sites. I have an issue with their combination. These rules work fine independently for me on different websites / servers
My standard force to WWW. rule is:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But I have this combined with a mod_rewrite to turn an address:
www.site.com/folder1/folder2
into:
www.site.com/category.php?catcode=folder1&pagetype=folder2
The code for this is:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]*)/([^/]*)/?$ /view_category.php?catcode=$1&page=$2 [NC,L]
Both these code parts work on their own, but I have an issue when I try to reach this address:
http://example.com and it returns:
http://www.example.com/view_category.php?catcode=http:&page=www.example.com
Which is clearly not acceptable. What should be achieved is:
http://www.example.com [/index.php]
Why is this not appearing to treat the Redirect 301 as a [L]ast request?
Things I have read/tried already:
htaccess www redirect for seo friendly url
htaccess non-www. to www redirect AND request rewrite to index.php
Also I have tried to define that the mod_rewrite rule only applies to www. only, so:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^www.%{HTTP_HOST}([^/]*)/([^/]*)/?$ www.%{HTTP_HOST}/view_category.php?catcode=$1&page=$2 [NC,L]
But this does not change the behaviour. the www. redirect works but it continues to try and load the mod_rewrite when there is no www. , the address http://example.com/ does the same thing, but the addresses http://www.example.com and http://www.example.com/ both behave correctly, which to me looks like the issue is not specifically the mod_rewrite.
I have also tried substituting [L] for [END] but with no difference in result.
( I have also tried removing the options -indexes just to see but this also changed nothing. )
The process works perfectly when used as intended, http://example.com/folder1/folder2 redirects correctly (to http://www.example.com/folder1/folder2).
My .htaccess:
ErrorDocument 400 /error/400.php
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /view_category.php?catcode=$1&page=$3 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]*)/([^/]*)/?$ /view_category.php?catcode=$1&page=$2 [NC,L]
<Files php.ini>
order deny,allow
deny from all
</Files>
What have I missed?
If I'm not mistaken, you need to change ([^/]*) to ([^/]+).
The one you're using makes it match 0 or more characters, whereas the alternative makes it match 1 or more characters.
As you have made the trailing slash optional, a request to the root of the domain will match ([^/]*)/([^/]*)/?$.
So, your file should now look like this:
ErrorDocument 400 /error/400.php
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /view_category.php?catcode=$1&page=$3 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /view_category.php?catcode=$1&page=$2 [NC,L]
<Files php.ini>
Order Deny,Allow
Deny From All
</Files>
I tried using this code on .htacces to have my own custom 404 on my page,.
ErrorDocument 404 http://mysite.com/404.shtml
when i visit the page that does not exist on the site. it must show something like this
but this is the one it shows
here is my complete htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
php_value memory_limit 170M
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysite.com.*$ [NC]
RewriteRule ^(.*)$ http://mysite.com%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://mysite.com/404.shtml
The location of my htaccess is located along with the 404.shtml and the index.php of the site
Removing this code would fix the problem. How can i make it work with routing.
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
Instead of referencing the entire url with ErrorDocument 404 http://mysite.com/404.shtml, name the document relative to your web root. In this case, /404.shtml.
Use these codes in your .htaccess for capture 404 error pages.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>
Now your all 404 requests will goto /404.html. You can also use /404.php to code more dynamically. Also make sure that rewrite module is enabled in your apache.