redirect a page to an url with # - apache

I would like to redirect an url (let's say https://www.example.com/de/page-a/) to an url having a parameter with a # (https://www.example.com/de/page-b/#filter:fields=6).
I don't find the right rule using RewriteRule or Redirect. It always redirects to https://www.example.com/de/page-b/, ignoring the last part.
Can someone help me on that?
Best regards

Redirect ^de/page\-a/$ exemple.com/de/page-b/#filter:fields=51? [L,R=301]
You are mixing up the directives. The mod_alias Redirect directive takes a simple root-relative URL prefix (starting with a slash) as the source URL argument. So the above will never match and nothing happens. There are no [L,R=301] flags with the Redirect directive, which is used by mod_rewrite RewriteRule.
For example:
Redirect 301 /de/page-a/ /de/page-b/#filter:fields=51
You do not need to specify an absolute URL as the target if you are redirecting to the same scheme + hostname.
When redirecting to a fragment identifier (everything after the #) you do need to be careful of redirect loops since the fragid is not passed back to the server. In this case you are OK since you are redirecting to a different URL-path, ie. page-a to page-b. But you could not redirect from page-a to page-a (same URL-path and query string) and simply change the fragid as it will create a redirect loop. For this you would need to use JavaScript.
NB: Test with 302 (temporary) redirect first to avoid caching issues.
If you want to implement this using mod_rewrite (ie. RewriteRule) - perhaps if you are already using mod_rewrite - then you could do the following instead:
RewriteEngine On
RewriteRule ^de/page-a/$ /de/page-b/#filter:fields=51 [NE,R=301,L]
Note that there is no slash prefix on the RewriteRule pattern.
The NE flag is required here in order to prevent the # being URL-encoded in the response and treated as part of the URL-path.

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.

Apache rewrite path to paramater

How would i convert:
https://www.example.com/ja to redirect to https://www.example.com/?lng=ja
I've tried the apache rewrite rule:
RewriteRule ^ja$ /?lng=ja&%{QUERY_STRING}
but I just get a 404 when accessing https://www.example.com/ja
I've also tried:
RewriteRule ^ja$ /?lng=ja
Which just loads the home page with /ja but doesn't seem to add on the url param
Try RewriteRule ^ja$ /?lng=ja [R,L]
By default, mod_rewrite maps a URL to a filesystem path, but it can also be used to redirect one URL to another URL.
Use of the [R] flag causes a HTTP redirect to be issued to the browser. If a fully-qualified URL is specified (that is, including http://servername/) then a redirect will be issued to that location. Otherwise, the current protocol, servername, and port number will be used to generate the URL sent with the redirect.
You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.

Htaccess internal redirect from domain to another url

Yet another .htaccess rewrite question... But can't find a way to make it work
My goal :
http://one.domain.com -> http://another.domain.com/specific/url
I'm not looking for a redirect, but a rewrite.
one.domain.com is a vhost alias on another.domain.com vhost.
No need for URL rest ($1...) it can be a perfect match (one-page site).
My best shot below makes a redirect, which I don't want.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^one\.domain\.com$
RewriteRule ^(.*)$ http://another.domain.com/specific/url [L]
This cannot be done using RewriteRule - from the Apache docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as a
URL-path. Otherwise, an external redirect is performed for the given
URL.
You may want to use a workaround, e.g. reading the page http://another.domain.com/specific/url with Curl and then outputting the contents using echo() (if you're using PHP). Anyway it looks like a hard-to-maintain solution.

Exclude a URL from folder redirection

I am redirecting all URLs from www.example.com/forums to www.example.com/blog/.
so I made this rule in .htaccess:
RewriteRule ^forums blog/$1 [L,R=301]
the thing is that I want to exclude some URLs that also begin with forums/ and redirect them to particular URL other than /blog.
For example, forums/8/some-made-up-word-here-1681 to /studies/some-made-up-studies.
Right now, it redirects to /blog like all URLs that start with forum/
You just need to include the more specific redirects first, before the more general rule. For example:
RewriteEngine On
# Specific redirects
RewriteRule ^forums/8/some-made-up-word-here-1681 /studies/some-made-up-studies [R,L]
# Redirect all other URLs that start /forums
RewriteRule ^forums/?(.*) /blog/$1 [R,L]
I've also modified your existing directive to redirect /forums/<something> to /blog/<something>, which I assume was perhaps the original intention, since you were using a $1 backreference in the substitution, but did not have a capturing group in the RewriteRule pattern. Your original directive would have redirected /forums/<something> to /blog/.
I've also included a slash prefix on the substitution. This is required for redirects, although you may have set RewriteBase instead, in which case you do not need to do this.
You will need to clear your browser cache before testing, since the earlier catch-all 301 will have been cached hard by the browser. For this reason it is often easier to test with temporary 302s in order to avoid the caching problem. Change the above temporary redirects to 301s only after you have confirmed this is working as intended.
UPDATE: To redirect all URLs that start /forums to /blog/, without copying the remainder of the URL, then change the last directive to read:
# Redirect all other URLs that start /forums
RewriteRule ^forums /blog/ [R,L]
Basically, the $1 in your original directive was superfluous.

Redirect all pages from old domain to new specific URL without trailing slash content

I've got an old domain, let's say oldomain.com where I need to redirect all traffic to a specific URL, newdomain.com/path
While redirects from oldomain.com go perfectly, anything with content after the trailing slash will be copied over in the newdomain url structure causing 404's.
For example visiting: oldomain.com/somepage will result in newdomain.com/pathsomepage
What I'm looking for are some rewrite rules that will redirect any and all traffic from oldomain.com to newdomain.com/path without changing the specific "newdomain.com/path" URL.
I'm currently using the rules bellow which leads to the result above:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldomain.com
RewriteRule ^(.*) https://newdomain.com/path [P]
PS: the redirect is going to a Magento store.
You are trying to reverse-proxy in your directives (the P flag in the rewrite), but since you are describing a redirect... In the old virtualhost you just need to add a simple Redirect directive like this:
Redirect / https://newdomain.example.com/
This will Redirect all requests no matter how they are made to the new domain. Example GET /something will be redirected to https://newdomain.example.com/something
If you want the target to be a fixed destination like https://newdomain.example.com/path no matter what, use RedirectMatch instead:
RedirectMatch ^ https://newdomain.example.com/path