How to create htaccess 301 redirect to a new address with a hash in the url (to i.e. example.com/#clients) - apache

I made a onepage site and now I want to redirect the old links to the homepage, and preferrably to the correct section.
How can I make a htaccess 301 redirect to a new address with hash un url (i.e. example.com/#clients)
Is it ok for search engines/google to have the hash? Does it matter?
Thank you.

The hash part of a URL never reach the webserver. When the client browser sends a URL request to a web server it sends everything up to the hash sign, so you wont get the hash server side.
If you want to escape it in your redirect URL just use the NE flag appending [R=301,NE,L] to your rewriterule.

On your old site put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^ http://www.newsite.com/#clients [L,NE,R=301]
Having # in URL won't cause any problem with your SEO ranking.

I combined some tutorials and examples and got it working with this code:
RewriteRule ^en/contact.*$ http://domain.com/en/#contact [R=301,NE,L]
Which redirects from http://example.com/en/contact
--->
http://example.com/en/#contact
It works.
But is this somehow different to anubhava's example:
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com$ [NC]
RewriteRule ^ http://www.newsite.com/#clients [L,NE,R=301]

Related

How do I redirect all traffic to a new domain except for the home page

So I'm trying to redirect traffic that goes to other pages using htaccess from a domain to a new subdomain but I'd like to keep traffic to the homepage there.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http\:\/\/newsubdomain\.domain\.com\/" [R=301,L]
This just redirects everything to the new subdomain. That's where I'd like some help, how can I keep the traffic that comes to the homepage on olddmain.com and redirect queries to olddomain/post and have them go to newsubdomain.domain.com/post
Thanks
This just redirects everything to the new subdomain.
No it doesn't. (If everything is being redirected then either something else is doing that or you are seeing a cached redirect.)
The directives as posted actually do the exact opposite of what you're trying to do. These directives only redirect the homepage (ie. requests for the document root).
To redirect everything, except the homepage, you can do something like the following at the top of the .htaccess file:
# Redirect everything except the homepage
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule !^$ https://newsubdomain.domain.com%{REQUEST_URI} [R=301,L]
The RewriteRule pattern !^$ matches everything except the root (an empty URL-path).
You will need to clear your browser cache before testing.
Test first with 302 (temporary) redirects to avoid potential caching issues.
Additional notes:
There is no need to backslash-escape colons, slashes and dots in the RewriteRule substitution string (2nd argument) - this is an "ordinary" string, not a regex. (This unnecessary escaping is quite typical of an attempt at using cPanel's "redirect" generation.)
I assume this should be HTTPS, not HTTP?
The two conditions (RewriteCond directives) can be combined into one.

External and internal redirection using htaccess

I want to convert http://mywebsite.com/folder/file.html to http://mywebsite.com/file.
I am not using regular expression as first I am only concerned about this one URL only.
I tried this-
Redirect 301 /folder/file.html http://mywebsite.com/file
Using this I am able to externally redirect this URL to the desired one but since the URL doesn't exist so I am getting 404.
Now, in order to internally redirect the new URL to the old one, I am using below command but it doesn't seem to work-
RewriteRule http://mywebsite.com/file http://mywebsite.com/folder/file.html [L]
Use only mod_rewrite directives and use THE_REQUEST variable for external redirection.
# turn on rewrite engine
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+/folder/file\.html[\s?] [NC]
RewriteRule ^ /file [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^file/?$ /folder/file.html [L,NC]

%25 in being appended in htaccess redirected Url

i am tried o redirect all non https url to http so i am using this in .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [B,R,L]
but when i remove https from url for testing , it redirect to https but it goes to https://example.com/%25myfile.php
that %25 is being added in url and making link NOT FOUND . am i making some mistake in above? I followed Apache: %25 in url (400 Bad Request) and added flag B as shown in above code but it not work .
Thanks .
Change your rule with this rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [NE,R=301,L]
NE flag prevents encoding special characters.
%{REQUEST_URI} uses original URI not relative to current directory.
Make sure to clear your browser cache before testing the change.

How to rewrite url and redirect with apache mod rewrite?

I have got url:
ipaddress/panelname/main/index.php
How to rebuild it to
ipaddress/center/index.php
?
ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.
I tryed to do this in .htaccess:
RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1
but i don't see redirect from panelname to center.
but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)
How to rewrite all to another links and howto see redirect from old links to my new? Thank you.
RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.
Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).
You either have to do the following:
RewriteRule ^panelname(.*)$ /center$1 [R,L]
or:
RedirectMatch 302 ^/panelname(.*)$ /center$1
Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.
If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).
RewriteEngine on
# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]
# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]

Strange behavior of htaccess redirect

My goal is to redirect an entire domain to another. Every URL of the old domain shall redirect to the root URL of the new domain.
To achieve this I'am doing:
redirect 301 / http://www.google.de/
Problem is that when I test it on http://localhost/randomPath then it redirects to http://www.google.de/randomPath, but not to the root URL of google.de.
Does anyone know how to solve this?
Use mod_rewrite for finer control on rules like matching Host name etc.:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.google.de/? [L,R]
? in the end of target URI will strip off any existing query string.