Apache rewrite path to paramater - apache

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.

Related

redirect a page to an url with #

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.

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.

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

Redirect domain based on regular expression

I need to change domains when i have a certain path on the url request. More specifically i need to identify first if a certain path (/path) exists on the url request. If exists then show the website with the domain www.mynewdomain.com/path/. If /path does not exists on the url request then show www.myolddomain.com or whatever the request will be.
You can use RewriteRule to redirect the request to the new domain. You can mention the config in the virtual host.
RewriteEngine On
RewriteRule /path(.*) http://www.mynewdomain.com/$1 [L,R]
This configuration retains the URL part after the /path token [ denoted by (.*) ] and is rewritten to the redirected path using $1.

How to hide a rewrited URL in web browser

I used Apache's mod_rewrite to redirect HTTP request to another server. I want to keep original URL in client's browser. Now it displayed rewrited URL in client's browser. The configuration as below in Httpd.conf file. Thank you advance.
RewriteEngine On
RewriteBase /tpiaccs/
RewriteRule /tpiaccs/uat/(.*) http:///tpiaccs/uat/$1 [NC,L]
If I recall correctly, by default it rewrites using external redirection (an implicit [R] flag) unless you specify otherwise.
This might do what you're after:
RewriteRule /tpiaccs/uat/(.*) http:///tpiaccs/uat/$1 [P,NC,L]
The P should make it internally proxy the request rather than doing an external redirect.
See https://httpd.apache.org/docs/trunk/rewrite/flags.html for details on the flags that can be used with URL rewriting.