301 redirect of an url that contains a hashtag replaces the hashtag with %23 in the browser address bar. - hashtag

I tried a 301 redirect in CPanel but when the redirected url appears in the browser address bar it has the hashtag changed to %23.
With this change the final destination is never reached. Here is the URL:
https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=ace%20hardware%20norcross%20ga&lrd=0x88f5a08e7fc35a07:0xe1ccba95a38c346b,1,,
Can anyone find a solution?

If you create a 301 redirect using cPanel, it uses default rewrite rule modifiers and the URL will be encoded.
Check your .htaccess file, find that 301 rewrite rule. It should look similar to this:
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Add a modifier so it looks like this:
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
The NE modifier will disable encoding.

Related

.htaccess redirect not redirecting

I am trying to Redirect pages to new location on the same website using .htaccess
the physical file name is displayitems.php but there is a rule in .htaccess
RewriteRule ^buy-online-(.*) ./displayitems.php?url=$1
which is to handle the user friends URLs and works well.
Now i want to redirects these user friendly urls to new location which is on the same website for eg.
redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]
redirect https://example.com/buy-online-alhabib-rings3-sku-1658906162 https://example.com/products/jewelry/buy-online-alhabib-rings3-sku-1658906162 [R=301]
redirect https://example.com/buy-online-alhabib-rings2-sku-1658906161 https://example.com/products/jewelry/buy-online-alhabib-rings2-sku-1658906161 [R=301]
redirect https://example.com/buy-online-alhabib-rings1-sku-1658906160 https://example.com/products/jewelry/buy-online-alhabib-rings1-sku-1658906160 [R=301]
these user friendly url doesn't have any extensions like ".php" ".htm" etc
but nothing happening.
I have added this code in php file to check if url doesn't contain \products\ than redirect it to new location with the same name, for testing i just redirect it with 302 once all tested i will change it to 301
if (strpos($_SERVER['REQUEST_URI'], "/products/") === false) { $NewAddress = strtolower("Location:". $ini['website_address_https'] . "products/".$Product['categoriesname']."/".$Product['BrandName'].$_SERVER['REQUEST_URI']); header("$NewAddress",TRUE,302); }
redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]
There are 3 main issues here:
The mod_alias Redirect directive takes a root-relative URL-path (starting with a slash) as the source URL, not an absolute URL. So the above will never match.
You have mixed syntax with mod_rewrite. [R=301] is a RewriteRule (mod_rewrite) flags argument and has nothing to do with the mod_alias Redirect directive. Redirect takes the HTTP status code as an optional second argument. eg. Redirect 301 /buy-online-alhabib-rings4-sku-1658906163 ...
Since you are using mod_rewrite (ie. RewriteRule) for your internal rewrite, you should use mod_rewrite for external redirects as well to avoid potential conflicts. These redirects then need to go before your internal rewrite.
Additionally,
In the 4 redirects you have posted it looks like you are simply injecting /products/jewelry at the start of the URL-path. This does not need 4 separate rules, providing you are wanting to redirect all URLs that following this particular format.
Try the following instead:
RewriteEngine On
# Inject (redirect) "/product/jewelry" at the start of the URL-path
RewriteRule ^buy-online-alhabib-rings\d-sku-\d+$ /products/jewelry/$0 [R=301,L]
# Internal rewrite
RewriteRule ^buy-online-(.*) displayitems.php?url=$1 [L]
The $0 backreference in the first rule contains the entire URL-path that is matched by the RewriteRule pattern.
Note that I also removed ./ from the start of the substitution string in the last rule. This is unnecessary here.

Redirecting some sub pages to another format page using htaccess

I would like to add 301 redirects to specific type subpage on my site which is hosted on LAMP server.
for example,
example.com/witcher-3-100309/player-count - example.com/witcher-3-100309?tab=player-count
example.com/dota-2-100209/player-count - example.com/dota-2-100209?tab=player-count
example.com/pubg-300100/player-count - example.com/pubg-300100?tab=player-count
Is there any way in htaccess to write a general rule for all these type URLs to redirect correctly instead of individual 301 redirect codes in htaccess.
Thanks in advance.
The following should do it
RewriteEngine on
RewriteCond %{THE_REQUEST} /.+\?=([^\s]+) [NC]
RewriteRule ^.+$ %{REQUEST_URI}?tab=%1 [NE,L,R]
This will redirect /foobar/?q=value to /foobar/?tab=value .
When you sure the rule is working ok you can change the R (Temp Redirect flag) to R=301 to make the redirection permanent.

How to redirect a specific page to home page in .htaccess?

I want to redirect a specific page to home page.
My source URL is http://examlple.com/index.html?page=22
My target URL is http://examlple.com/
I have tried Redirect /index.html?page=22 http://example.com/
also I have tried by using 301 and RewriteRule but no success yet, this is not working.
You can't match against querystrings in Redirect directive.
Try mod_rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^page=22$
RewriteRule ^index\.html$ http://example.com/? [NC,L,R]
Empty question ? mark at the end is important as it will discard the original query string from url.

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.

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

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]