.htaccess redirect within root directory - .htaccess

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 !!

Related

.htaccess redirect: correcting image path from localhost/folder to domain.tld [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

Redirect custom URLs via htaccess

I have my own domain:
http://cesarferreira.com
and I wanna make
http://cesarferreira.com/github
point to
https://github.com/cesarferreira
Without having to make a /github/ folder with an index.html with a redirect for each page I own (facebook, twitter, pinterest, etc)
Is there a way like for example htaccess catchig *.com/github and pointing to a given static url?
Thanks a lot
If your document root serves -
http://cesarferreira.com
you can put a redirect in .htaccess like -
Redirect /github https://github.com/cesarferreira
Take a look at URL rewriter 'http://httpd.apache.org/docs/2.0/misc/rewriteguide.html'. That should be able to do everything you want and more.
As long as it is enabled in apache then you can use it in .htaccess files also.
You can use mod_alias:
Redirect 301 /github https://github.com/cesarferreira
Or if you only want github to point only to the folder:
RedirectMatch 301 ^/github https://github.com/cesarferreira
You can put that in the htaccess file of your document root.

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.

RedirectMatch issue (redirecting folder, but not contents inside folder)

I have tried this every which way I could, but am not getting any love. I am attempting to redirect a subfolder in the site to just a static page. But I do NOT want to redirect anything ELSE that is inside said folder.
I have tried using the regular redirect 301, but of course that redirects everything in the subfolder.
To sum up: I want to redirect http://www.domain.com/folder/ to http://www.domain.com/foldername2/file.php without redirecting anything else inside /folder/.
The following is what I have in my htaccess... but... it is just ignoring it completely.
RedirectMatch 301 ^/folder$ http://www.domain.com/foldername2/file.php
Thanks in advance for help.
Instead of relying on HTTP, you can use a nearly empty index.html file which contains a <meta> refresh tag in the head section of the document.
The wikipedia article on meta refresh has some good examples, e.g.
<meta http-equiv="refresh" content="0; url=http://example.com/">
The 0 means "zero seconds"—that is, immediately.
After much struggling, I found the answer! It does not use the RedirectMatch, however. It uses a rewrite condition and rule instead.
RewriteCond %{REQUEST_URI} ^/folder/$
Rewriterule ^(.*)$ http://www.domain.com/foldername2/file.php [L,R=301]

Redirecting bad url links from external sites with .htaccess

I have two inbound links that land on 404 pages because the links are squiffy. I have tried 301'ing these links as normal but without success, I believe because of the characters used from this external URL.
Column<u>Radiators.html
is the page suggested on this external site and
Column_Radiators.html
is the actual page.
also
Bath%3Cu%3EFiller.html
on the external site and
Bath_Filler.html
on our actual website.
How can I succesfully redirect these pages?
Simply add these lines in your .htaccess
redirect 301 /Column<u>Radiators.html /index.html
redirect 301 /Bath<u>Filler.html /index.html
Note: I use index.html as the default redirection but you can use a custom 404.

Resources