Redirecting nonexistent index.html to home page in Wordpress is causing too many redirects in browser - html

I recently changed my website from static html to Wordpress, and in doing so I've been redirecting all of my former and nonexistent pages with my .htaccess file whenever Google shows me a crawl error. I've been successful in redirecting all crawl errors until today. My old index.html is throwing a crawl error and when I use:
Redirect 301 /index.html http://www.example.com
... I get this from my browser:
Too many redirects occurred trying to open www.example.com. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page.*
I have since removed the above redirect from my .htaccess file and will just live with the crawl error if I can't get this resolved. But I'm thinking somebody knows how to solve this and if so, I'd really appreciate it if you'd let me know how.

You could experience this redirect loop if your DirectoryIndex is set to index.html (as the first option), which is likely to be the default setting on your server.
Basically, when you access a directory, eg. http://example.com/ (the root directory) then the DirectoryIndex directive tells the server which file to serve (via an internal rewrite). eg. http://example.com/index.html. This then seeds the redirect loop.
Since you are using WordPress, you could change this in .htaccess to favour index.php instead. For example, at the top of your .htaccess file:
DirectoryIndex index.php index.html
However, you could also solve this by using mod_rewrite (which is probably preferable). In fact, since WordPress already uses mod_rewrite (eg. RewriteRule directive) then you should also be using mod_rewrite for your redirects, not mod_alias (eg. Redirect directive). You should not mix redirects from both modules in .htaccess since you can easily get conflicts. Different modules execute at different times, regardless of their order in .htaccess.
By using mod_rewrite you can avoid a redirect loop by checking against THE_REQUEST. For example:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html [NC]
RewriteRule ^index\.html$ / [R=301,L]

Related

How do you make a subpage of a subpage for a website using only cpanel that hides the .html file extension?

I have a website with subpages that have URLs which look something like this:
https://www.example.com/hello/world
https://www.example.com/hello/earth
In cpanel, my file structure for those pages looks something like this:
[folder]
hello
└ [folder]
world.html
earth.html
.htaccess
index.html
My .htaccess file has the following rule to account for missing .html file extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
How do I make https://www.example.com/hello load as a working webpage?
I have tried simply adding hello.html to the folder to no avail -- like so:
[folder]
hello
└ [folder]
world.html
earth.html
.htaccess
hello.html
index.html
When I attempt to access https://www.example.com/hello in that example, it leads me to my 404 page, but .../hello/world and .../hello/earth still work. https://www.example.com/hello.html does work, but I don't want the .html file extension to be visible to the end user.
I have yet to find a solution to hide the .html file extension in this situation. Help in doing so would be appreciated!
When I attempt to access https://www.example.com/hello in that example, it leads me to my 404 page
Because you have a directory of the same name and (by default) mod_dir will issue a 301 redirect to fix/append the trailing slash. The 404 then results from the redirected request when your directive attempts to append the .html file extension by rewriting the request (now with a trailing slash) from /hello/ to /hello/.html (which naturally results in a 404).
To prevent mod_dir appending a trailing slash to requests for directories you can include the following directive at the top of the file:
# Prevent mod_dir appending a trailing to directory requests
DirectorySlash Off
# Disable auto-generated directory listings (mod_autoindex)
Options -Indexes
You will need to make sure your browser cache is cleared before testing since the earlier 301 (permanent) redirect (by mod_dir) will have been persistently cached by the browser.
For security, you also need to ensure that auto-generated directory listings are disabled (unless you explicitly want this behaviour), since when DirectoryIndex Off is set and you request a directory without a trailing slash, mod_autoindex will still generate a directory listing even if a Directoryindex document is present in that directory.
Alternative solution
Alternatively, you can still allow mod_dir to append the trailing slash (so a request for /hello is still redirected to /hello/ and so /hello/ is the canonical URL), but allow for an optional trailing slash in the RewriteRule pattern, but exclude this from the capturing subpattern by using a non-greedy regex.
For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+?)/?$ $1.html [L]
With a request for /hello/ then hello is captured by the parenthesised subpattern, so it is rewritten to hello.html as above.

"public_html" automatically appearing after folder name in URL and resulting in 404

I was fiddling around with the .htaccess in my web hosting directory, attempting to force HTTPS to my URLs to activate my SSL that comes with Hostgator (which did in fact work), but after changing stuff in there and whenever I try to access my page by simply typing in the URL bar examp.com the browser automatically adds public_html after it, which results in a 404, and only when I remove the public_html part of the URL am I able to access the HTML documents and folders. But if I type https://www.exampe.com THEN it works properly without adding anything. I was unable to find anything regarding this problem and it happens with every browser.
My .htaccess file looks like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1
Even after removing it the problem persists.
EDIT: The foulder structure is home3/example/.htaccess
EDIT: I should probably mention this, when I originally edited the .htacces file in the first place it was not present, literally nowhere in my hosting directory on Hostgator, not even after checking "Show dotted files", so I ended up creating it manually and then I started fiddling with the code above.

301 redirect for html files in one directory only to Custom Post Type in Wordpress

I am struggling to get my head around an htaccess rule to redirect requests for an html file to go to a custom post. I have looked on here and in other places and nearly got there.
I want to redirect ONLY mydomain.com/profiles/.html to mydomain.com/name_profile/
So mydomain.com/profiles/smith.html to mydomain.com/name_profiles/smith. There are some 900 html files to be redirected and they are all contained in this directory. Other html files in the domain I need to leave correctly associated.
I currently have
RedirectMatch 301 ^/([^/]+)/([^/.]+)\.html$ /$1/$2/
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+)\.html$ /$1/$2/$3/
But this redirects all html pages not just the ones in the profiles directory.
I am new at htaccess and have found several tutorials but none at a level I can understand, so any help is most welcome.
Place this rule just below RewriteEngine On line:
RewriteRule ^profiles/([^.]+)\.html$ /name_profiles/$1 [L,NC,R=301]
Use this rule:
RewriteEngine On
RewriteRule ^profiles/([^.]+)\.html/? name_profiles/$1 [DPI,L,R]
Or better, remove your RewriteMatch rules and replace with this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/.]+)\.html/? $1/$2 [DPI,L,R]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html/? $1/$2/$3 [DPI,L,R]
This assumes that mod_rewrite is both installed and activated for htaccess files.
If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo();
By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf
and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Server side changes in Angular html5 mode

I was wondering if any one can tell me what are the server side changes which needs to be done when you change your mode in to html5 in Angular js. because When I did tried to change the it to html 5 mode I was unable to go in to my inner html pages. in Angular API it says that suers need to do a server side changes as well.
what are the server side changes
do we need to do any other changes as well ?
You should distinguish two type of calls:
HTML calls from the browser. These should be all remapped to serve your index or whatever.
JSON API requests. These should pass through to your app server.
How to distinguish between these two types of calls, and how to remap the former, depends strongly on your setup.
If you are using nginx, for instance, a combination of checking $http_accept to be application/json (see http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Requests and http://wiki.nginx.org/HttpCoreModule#.24http_HEADER) and rewrite (http://wiki.nginx.org/HttpRewriteModule#rewrite) you can achieve what you want.
You need to setup your server to rewrite everything to index.html per: https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode ...
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
</VirtualHost>
My app has params passed to a controller (via ui-router) so before html5mode I would goto
www.blah.com/angapp/#/myUIrouterController?param_x=1&param_y=2
Guessing browsers know that /#/ folder path part should serve index.html.
Now that # would be gone with html5mode, the server by default doesn't know to serve the index.html for that folder since the url will just be:
www.blah.com/angapp/myUIrouterController?param_x=1&param_y=2
myUIRouterController isn't a real file so the server would just serve a 404, Hence why I think the rewrites are needed so it knows to send everything to index.html (so above in combo with the <base> tag should work... note: requireBase is optional but heard it helps older browsers like IE9 maybe).

Default 403 page not showing up

I recently set up a few new folders in my server, but I wanted to turn them and all their subfolders forbidden. So, using .htaccess, I simply used deny from all. The issue here is that it's redirecting me to my host's 404 page (which has a lot of advertisements, join us, it looks like their front page), instead of the regular 403 page.
This seems like a very trivial question, but I couldn't find the answer anywhere. How do I get everything in that folder to use the classic 403 page, being this:
<html>
<head>
<title>403 Forbidden</title>
</head>
<body>
<h1>Forbidden</h1>
<p>You don't have permission to access /example/example2/exampleC/
on this server.</p>
</head>
</html>
Which changes depending on which folder I just entered. If In /example/ I have /example1/, /example2/ and /example3/, how do I make it so that if I enter /example1/, I get a 403 page that says I don't have access to /example/example1/?
This may seem a bit overly (needlessly) complicated.
let's debug step by step
for debugging please follow these steps
Rename in your DocumentRoot .htaccess to .htaccess_safe
Create a folder in DocumentRoot "test"
Insert in that folder a .htaccess with following content
RewriteEngine On
RewriteRule ^(.*)$ http://google.de [L,R=301]
Open in your Browser http://example.com/test/ [you should redirect to google]
If this works ... read more under step 5.
If not ... put this .htaccess in the DocumentRoot Folder and open in your Browser http://example.com/test/ if this doesn't run too, stop debugging. If it works, your Server isn't able to exec .htaccess in subfolders (check for that the rights of the .htaccess file on your server group, user and chmod .....
(5) Rename your .htaccess_safe back to .htaccess and change line 12 RewriteRule ^$ http://www.avatarchan.net/site/ [R=301,L] to #RewriteRule ^$ http://www.avatarchan.net/site/ [R=301,L] and try again