How to fix my htaccess, it's redirecting my pages all to one index - html

I was on live chat with my domain support and they managed to delete the default htaccess file, now every url entered goes to my main index, what can I do to fix this?
Example:
domain: example.com
If I have a folder under example.com/folder
and go to it, the index for example.com shows instead of for that folder and if I put the path directly it stays in the browser but it shows the index for example.com always.
What can I write in my htaccess to fix this to how it used to be and keep mod rewrite enabled?
Edit: also, every time I've asked them to fix it or reset it to default they claim that it's a web development issue and not theirs. All they do is tell me to contact my developer.

hopefully, it works
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Related

Need to redirect missing URLs to new host

I'm moving a blog from one site to another and repurposing the original site. I want to maintain all existing links that point to the site and hopefully maintain SEO page ranking.
Old URL: http://www.companyabc.com/2010/04/test.html
New URL: http://blog.companyabc.com/2010/04/test.html
The way I'd like to do it is to use a custom 404 error page on www.companyabc.com like this:
<html><meta http-equiv="REFRESH" content="0;url=http://blog.companyabc.com/%1"></html>
where %1 is the original URI (/2010/04/test.html), but I don't know if that's possible.
Another option is to use an .htaccess file that redirects if the URL is not found, but I haven't gotten that to work either. I'm sure I'm doing something wrong in the rewrite condition:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.*)\.html$ https://blog.companyabc.com/$1.html [R=301,L]
Any suggestions? Thanks for the help.
I got it working using the following .htaccess configuration:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://blog.companyabc.com/$1 [R=301,L]
I never tried the REQUEST_FILENAME value because I mistakenly thought it only applied to files that can be downloaded from the website. I didn't realize it also applies to the .html files in the blog.
With this solution all URLs that don't exist at www.companyabc.com will be redirected to blog.companyabc.com instead of showing a 404 error page, which is what I'm looking for.

Shortten URL's with htaccess that works with Google indexing

Ok, let's explain first what I have in the server and in the htaccess file:
In the server I have the following files:
www.mydomain.com/provincias/madrid/town1.html
www.mydomain.com/provincias/madrid/town2.html
www.mydomain.com/provincias/madrid/town3.html
...
www.mydomain.com/provincias/barcelona/town1.html
www.mydomain.com/provincias/barcelona/town2.html
...
as you see, the folder 'provincias' is repeated in all urls, is necessary for get ordered the internal files but not for navigate and for users, for this reason I have added the following rule in the htaccess file that works fine:
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)\.html$ provincias/$1/$2\.html [L,NC]
so with this rule you can access, for example, the next url: www.mydomain.com/provincias/barcelona/town2.html with this other url that users see in the address bar of their browsers: www.mydomain.com/barcelona/town2.html because internally the htaccess redirect it to the real url with 'provincias'.
But I have a problem, I have seen that Google has indexed the technical and real url, the first one, with 'provincias' folder, and if you click on it people navigate watching that url in their address bar. How can I redirect the people and google traffic from www.mydomain.com/provincias/barcelona/town2.html to www.mydomain.com/barcelona/town2.html taking into consideration that internally the url without 'provincias' doesn't exist?
Try this rule in your .htaccess
#redirect real url to new one
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /+provincias/([^&\ ]+)/([^&\ ]+)\.html
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)\.html$ provincias/$1/$2\.html [L,NC]

webpage returning 403 error and not showing the actual page

I have a page on my website ( it's html ) which is named "providers.html".
in order to remove the .html part from url, I added this code to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
now the problem is that I have a folder which is named "providers" and contains files related to that page.
now when I go to the url, I get a 403 error because browser loads the folder and not the html page.
I really appreciate if any one can give me some guidance to solve this problem.
thanks
This might be what you're looking for:
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ([^\.]+) $1.html [NC,L]
Turned off directory slashes. This part is the most important part. Basically, if a directory exists, it shouldn't add a slash. This way, we can continue checking rules against exact URI input.
Added a check to see if the the current request does not end with a slash.
If it doesn't, rewrite to the respective HTML file
Otherwise, load up the contents of the directory. You'll more than likely want to have another .htaccess file in there to prevent listings etc., and only allow access to existing files.
Update: Removed ^ and $ from the rule, as I don't believe it is necessary here.

.htaccess Redirect all users to subfolder EXCEPT for IP

I've tried countless combinations from forums, answers here on Stack Overflow, and blogs. I can't get this to work right.
I'd like to redirect all users to a subdirectory - "/const" - except for an IP that I deem worthy.
This is what I'm using so far:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^91\.143\.253\.211
RewriteCond %{REQUEST_URI} !^/const/ [NC]
RewriteRule ^(.*)$ http://website.com/const/$1 [R=302,L]
And it redirects to the subfolder correctly, but it still redirects me, which is bad.
Little help?
You can use a Skip flag [s]. Here is an example with multiple IPs.
RewriteEngine on
RewriteBase /
RewriteCond %{REMOTE_ADDR} ^91\.143\.253\.211$ [OR]
RewriteCond %{REMOTE_ADDR} ^8\.8\.8\.8$
RewriteRule .? - [S=1]
RewriteCond %{REQUEST_URI} !^const [NC]
RewriteRule ^(.*)$ /const/$1 [R=302,L]
Note
In the example before I had %{REQUEST_FILENAME}. This typically only works on server config files where the path to the filename itself is visible to the redirection script. Here I've changed it to %{REQUEST_URI} the part of the request that the browser can actually access.
The rule that you have is correct. When I add those to a blank htaccess file, I get redirected to http://website.com/const/ as expected unless I change the first condition to my actual IP. Some things you should check:
Make sure you've flushed your browser's cache. Although 302 redirects shouldn't be cached in such a way, better to flush it just in case.
Make sure you are actually making a request from the expected IP (i.e. ^91\.143\.253\.211$). You can test this by adding this rule to the top of your htaccess file:
RewriteRule ^foo$ http://foo.bar/IP/%{REMOTE_ADDR} [L,R]
And if you go to http://website.com/foo you should get redirected to a non-existing site but with your IP in the URL. If that IP doesn't match 91.143.253.211 then you need to change your condition to match the right IP.

.htaccess rewriterule /state/city/

This will take a bit of explanation so I hope I don't lose everyone here.
I needed to get something like the following:
http://example.com/results.html?state=iowa&city=davenport
turned into:
http://example.com/iowa/davenport/
I was able to accomplish this with the use of these two rewriterules:
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1 RewriteRule
^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
The problem is that in the backend there is "some code somewhere" that is getting broken as a result of the second rewriterule. It has to do with filling in a select box based on the results of another one selected (I don't think that matters though). I think the problem is in that I'm modifying too broadly the /state/city.
Here is a copy of my full (modified for security) .htaccess file:
IndexIgnore *
AddHandler application/x-httpd-php5 .html .htm
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/USER
<Files php.ini>
order allow,deny
deny from all
</Files>
The code that its screwing up is very complex and its someone else's code. After a couple of hours I've been unable to wade through all of their stuff to even come close to what I may be able to change on their end to get things working.
Does anyone have ANY ideas on what I could do to avoid this problem? I really only have 3 .html files that I'm funneling my frontend code through so I had tried something like a
my rewriterules
and same with using just "files" instead of filesMatch. Everything I've come up with breaks something else or the entire site in one way or another.
First: (i) hostgator won't enable or give you access to rewrite logs; (ii) your suPHP config has syntax errors and hostgator almost certainly does spme of this and the .htacess / php.ini denials in its own root / vhost configs. However, I'll focus on the mod_rewrite elements:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
I am also assuming that you don't have any .htaccess files in subdirectories with the rewrite engine enabled as these could preempt this under rewrite "Per Directory" precedence rules.
Rules (3) is a simple domain redirector. Rule (4) is a draconian: redirect any URI which is not an existing file or directory to index.php in the current directory, but leaving the query string intact.
Rule (1) and (2) are your new rules. As Mike says, you should include the [L] but since the files cities.html and results.html exist it won't match anyway.
I am curious as to why the trailing slach in the URIs is optional. Better to decide and to fix this.
The issue is that the match criteria for (1) and (2) are two broad and are picking up URIs intended for the general catchall (4). You need to lock this down to make these mutually exclusive. One why is to mine your access logs (which are available with hostgator) to find the standard URIs which the application expects and check that none match (1) or (2) -- However, since most will include a ".", this probably isn't the case. But check.
The other issue is whether the existing scripts use absolute or relative references e.g. <img src="images/myimage.png"> in any output HTML. Here the browser has asked for http://www.example.com/texas/houston say and will therefore look for http://www.example.com/texas/images/myimage.png which doesn't match (1), (2) or (3) and therefore is caught by (4) and passed to /index.php. Ditto CSS files etc. Hence they won't 404 and index.php will get confused and send some default response which will hopelessly confuse the browser.
However, again analysis of the access logs (in this case or USIs with a referrer http://www.example.com/texas/houston) will show you if this is going on.
If your app uses standard subdirectories then you might be able to fix this by a rule (3.1) which looks something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1/$2 -f
RewriteRule .*?(images|css|styles)/(.+) $1/$2 [L]
though the details will depend on the rest of your application.
I was able to solve it by changing my (relevant) .htaccess entries to the following:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1
RewriteCond %{REQUEST_URI} !^/signup/
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The addition being:
RewriteCond %{REQUEST_URI} !^/signup/
HostGator was able to find that the issue was /signup somewhere in a log somewhere, never did find out which log they were able to look at but I assume it was something I didn't have access to.