301 redirect from index.html to /index/ - .htaccess

I have a big problem to redirect a website. It is about 4100 pages large and I need a solution to redirect all pages automatically. The problem is that there are to many pages to do every page manually. For example...
I have a structure like this
car.com/audi.html
car.com/ford.html
...
And I need to redirect to
car.com/audi/
car.com/ford/
But my problem is the big number of pages. Is there a command or something that can redirect all pages in one? Or have I to redirect every single page?

You can work around this
<html>
<head>
<title>CAR.COM</title>
<meta http-equiv="refresh" content="0; URL=LINK_TO_DIRECTORY">
<meta name="keywords" content="automatic redirection">
</head>
<body>
</body>
</html>

If you want to redirect .html requests to non-html (from old to new) you can use the following 1 liner redirect in htaccess :
RedirectMatch 301 ^/((?!index).+)\.html$ http://example.com/$1

Related

.htaccess redirect within root directory

I have copy and pasted the new index.html file I made into the old one. However, I need to now redirect users from the old .html files to the new index page which has anchor links. I have seen 301 redirects online but none of the examples included items that were in the same folder on the same domain. I tried to use the following, in a .htaccess file, but it didn't work.
//301 Redirect Old File
Redirect 301 /about_us.html #AboutUs
Redirect 301 /contact_us.html #GetQuote
Thanks in advance!
You simply need to prepend the target paths with a leading slash:
Redirect 301 /about_us.html /#AboutUs
This will redirect /about_us.html to /#AboutUs.
If you omit the leading slash, then Apache will mistake the target path for a comment, and throw an Internal Server Error.
You may need to redirect people to a different page because a URL has changed. Redirect to a Different URL using the Meta Tag "Refresh".
<meta http-equiv="refresh" content="0;URL=http://www.example.com/new-index.html">
You can change the amount of time that this redirecting page is displayed by changing the number in the refresh content="5 portion of the meta tag statement.
<meta http-equiv="refresh" content="5;URL=http://www.example.com/new-index.html">
Note that meta tags go in the <HEAD> </HEAD> section of the HTML
document.
You can also use this line of code Apache .htaccess redirect.
# Permanent URL redirect
Redirect 301 /web/old-index.html http://www.example.com/web/new-index.html
Hope this will help to resolve your issue !!

URL rewrinting from www.example.com to www.example.com/beta

His friends, currently my website is set up in the "beta" folder in my root just like
www.example.com/beta
Now if any user enters the following URL
www.example.com
then he should be automatically redirected to the first URL, any ideas? I know this can be done through .htaccess but i am not sure about it :( help me please. thanks
You'll find a good example of what you want right up at the top of When Not To Use Rewrite, which is a page everyone should read before they edit one character of an .htaccess file.
RewriteRule ^/(.*) http://other.example.com/$1
# is better expressed as..
Redirect / http://other.example.com/
Or, in your case,
Redirect / /beta/
Try adding
Redirect 301 / http://www.example.com/beta
to your .htaccess file in the root directory (untested)
Actually you can do it pretty simple (no .htaccess modification required).
Edit the headers of index.html file from your server main director (/).
Within the <HEAD> ... </HEAD> section add your redirection code, for example:
<meta http-equiv="refresh" content="0;URL=http://www.example.com/beta/somepage.html" />
Note that this should be located inside index.html of you main dir /.
For references see: http://devmain.blogspot.co.uk/2013/06/html-auto-redirect-website-to-different.html
Hope it help.

.htaccess rewrite without it affecting relative image/css/js URLs?

I need some simple help with my htaccess file, thank you in advance.
I have a number website URLs, such as:
www.site.com/index.php?page_path=solutions-overview.html
www.site.com/index.php?page_path=solutions-a.html
I want to use the RewriteEngine to change the above links to the following respectively:
www.site.com/solutions/overview
www.site.com/solutions/a
Below is the .htaccess code I am using:
RewriteEngine On
RewriteRule ^solutions/overview index.php?page_path=solutions-overview.html
RewriteRule ^solutions/a index.php?page_path=solutions-a.html
This works, however all of my images, CSS files and JS files no longer load because the page is trying to fetch the wrong URLs. For example "/images/blah.jpg" is instead loading as "/solutions/images/blah.jpg".
How can I modify the htaccess code to prevent relative URLs from changing?
Thank you!
Add this line <BASE href="http://www.yoursitename.com/"> to your page inside the <head> tag as follows:
<head>
<title>Your site title</title>
<BASE href="http://www.yoursitename.com/">
....
</head>

Mod Rewrite exclude folders

I have effectively used the rewrite rule to change my url from /teams.php?team=New York Yankees&year=2012 to teams/New York Yankees/2012.
My .htaccess file contains the following
RewriteEngine On
RewriteRule ^teams/([^/]*)/(^/]*)$ /teams.php?team=$1&year=$2 [L]
However, with this rewrite rule currently in effect, none of the external CSS and JS files will load. I've tried putting the following rewrite condition before the rewrite rule with no luck
RewriteCond %{REQUEST_URI} !^/(css/js)/
Almost all of my files have the following link to the external stylesheet.
<link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
Any ideas what I'm doing wrong here?
Thanks
You will have to use full paths when linking to your stylesheets, not relative paths like you have now. With the rewrite rules in place they become relative to the pre-rewritten URL.
For example, change:
<link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
to:
<link rel="stylesheet" type="text/css" href="/css/stylesheet.css" />
The reason they are not loading is because the browser is trying to access:
http://yoursite.com/teams/New York Yankees/2012/css/stylesheet.css
when you link to css/stylesheet which of course does not exist.

How can I get ErrorDocument in .htaccess to output wrong uri parameter?

I've worked-around the solution to transform all uri to one page via ErrorDocument.
The reason behind this is to send Files AND folders (nice urls) to one page. However, I need the URI string to be sent to index.php?uri=string . Is this possible via ErrorDocument, or how do I do this?
So I need to rewrite the
http://www.something.com/games/specific-game
to
http://www.something.com/index.php?uri=/games/specific-game
Is this possible at all, if, how?
If your server supports server-side include (SSI) then you use the following 404.shtml as your ErrorDocument:
<html>
<header>
<meta http-equiv="Refresh" content="0; url=/index.php?uri=<!--#echo var="REQUEST_URI" -->">
</header>
</html>
EDIT: there is a simpler way: create a PHP file as your ErrorDocument and you can do anything under the sun in it! :)
EDIT2: you can access the original URI using $_SERVER['REQUEST_URI']
EDIT3: heck, if you are just redirecting to index.php on the same host, you can just set that index.php file as your ErrorDocument and detect if the request is redirected from 404 error by checking if $_SERVER['REDIRECT_STATUS'] == '404'

Resources