is it not possible to two htaccess codes at once? - .htaccess

I have a website hosted on a free web hosting
it really messed my mind i put two codes in htaccess file one for 404 redirection
and other is for removing .html extension
in google chrome only one file is getting redirected on index.html [ i have total three.html files]
and in firefox no file is getting redirected its showing error and getting redirected on hosting website
when i put www.example.com/hello
it works fine
but when i put www.example.com/hello.html
it does not redirect on index page
same happens with if i type incorrect words it does not redirect on index.html
and when i remove second code (html extension remover) so 404 redirect code works fine
why?? what is the reason? is it not possible to add to 2 codes togather in htaccess file?
here is the code of htaccess--
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
ErrorDocument 404 /index.html
RewriteEngine on
RewriteBase /
RewriteCond %{http://www.example.com/new} !(\.[^./]+)$
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.example.com/new/$1 [R=301,L]

Related

htaccess 301 issuing problem with sub-directory creating a 404

I'm trying to redirect an url that gives me an error 404.
I need to redirect 'website.com/example/sub-example' to 'website.com/example2'
Note that in my .htaccess file 'example2' already exists because is and url for another redirection, but every time I run the url 'website.com/example/sub-example' it jumps to 'website.com/example2/sub-example' and gives me an 404. I don't want the sub-directory on the redirect URL. Wish I was clear enough. And if anyone help me because I really don't know what to do.
If can help I'm using WP and this one below is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
UPDATE: I resolved it. Basically “example2” was at the top of my redirected item so every time I tried to run ‘website.com/example/sub-example’ it was running ‘website.com/example2/sub-example’and eventually that page doesn’t exist. I simply moved every redirect with more than one subfolder (or subdirectory) at the top of the redirected pages and everything seems to work.
So after 8h of work I understood that the redirection pages works like works sex… “Dominate” who is at the top.

Redirect to homepage if file or directory doesn't exist using .htaccess

I am Hosting a domain on free hosting site 000webhost, I want to redirect all files or folder that doesn't exist to my homepage. The Code, I have used is:
RewriteEngine on
rewritecond %{http_host} ^domain\.com$ [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,l]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html
It works, but the url doesn't change to homepage. Like if I am trying to open www.domain.com/blaa it redirects me to home page but the URL is still www.domain.com/blaa. I want it to be changed to www.domain.com.
If I am trying to open a valid directory, which exist in my root folder, it also redirects to homepage. Like if I am trying to open www.domain.com/folder, it should result in files and directories in that folder, instead of that it redirects me to homepage and web address is still same like www.domain.com/folder.
Also I want to keep using the above 2 rules to open website with WWW and index.html with www.domain.com
Please help me.
Thanks in advance.
As per your questions's title, to redirect to homepage if the request is not for a file or dir, you can use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L]
You can also use ErrorDocument directive to rewrite non-existent requests to homepage
ErrorDocument 404 /

Change URL but show different resource

If none of my RewriteRule [L] matches, I want to redirect to a nice url /you/shall/not/pass, but show contents of /index.html.
This is what I am doing now (this is the very last rule in the file):
RewriteCond %{REQUEST_FILENAME} !-f # is not file
RewriteCond %{REQUEST_FILENAME} !-d # is not directory
RewriteCond %{ENV:REDIRECT_STATUS} !=200 # wasn't already redirected
RewriteRule .* index.html
It works fine, but keeps whatever garbage was written in the URL. I want it to be changed.
Doing this didn't work
#RewriteCond same as above
RewriteRule .* /you/shall/not/pass [R]
RewriteRule ^/you/shall/not/pass index.html
I apparentely don't understand how [R] works, whether it continues forwarding the changed url to other RewriteRules or not and what page it redirects to when the end of file is reached.
You can use ErrorDocument 404 with a rewrite rule for this:
Options +FollowSymLinks
ErrorDocument 404 http://domain.com/you/shall/not/pass
Then create a symbolic link like this:
/public_html/you/shall/not/pass -> /public_html/index.html
Replace /public_html/ with your DocumentRoot path.
So, after countless hours I made it work, somehow, I guess...
I am too tired to investigate, but I will get back later and edit if it is wrong.
ErrorDocument 404 http://domain.com/you/shall/not/pass # redirects if document does not exist
RewriteEngine on
RewriteRule ^$ /you/shall/not/pass [L,R] # redirects if request URI is empty, skips rest
RewriteRule ^you/shall/not/pass$ /index.html [L] # puts different resource to matched url
It's kinda weird that the solution is that short.. How come I didn't try that already?

301 Redirects on any non existent image, pdf or url using .htaccess

I have a website that is constantly updated and the problem is that the Google bot that indexes the website gives a lot of 404/500 errors when a image does not exist anymore or has been deleted. It's a product catalogue website, so products get deleted and added on a hourly basis.
I've tried this RewriteRule
^/(.*)$ /site/page/view/404 [R=301,L]
which causes all images on the website not to display any more or
^(.*) /site/page/view/404 [R=301,L]
which just gives error 500 on the browser.
Is there a way to match non existent files/urls and redirect them permanently to different url e.g. /site/page/view/404
Try this (assuming .htaccess is in web root)
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^.*$ /site/page/view/404 [R=301,L]
Alternatively, you can use ErrorDocument as well.
ErrorDocument 404 /site/page/view/404

htaccess ModRewrite URLs to pull contents from Index.php file, leaving URL as is

I am trying to run a mod_rewrite rule to load the contents of index.php regardless of the entered URL.
Then in the index.php file I will deal with either returning a correct page or outputting a 404 page.
This is for SEO purposes to allow a nice URL like:
http://www.example.com/product
rather than
http://www.example.com/?p=product
I have this working on one sub folder of my blog but when trying to test it on a new project all i get is 500 errors as for ALL urls.
Here is the existing htaccess:
RewriteEngine on
RewriteBase /products/
RewriteCond %{HTTP_HOST} ^www.danclarkie.co.uk/products [NC]
RewriteRule ^(.*)$ http://danclarkie.co.uk/products/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !/admin.php$
RewriteRule .* index.php
Any help ^_^
Dan

Resources