.htaccess redirect homepage only - apache

I need to 301 redirect homepage
https://example.com/
to
https://example.com/category/news
But I have a newsletter that uses this URL after submit:
https://example.com.pl/?na=s
So after basic 301 redirection, newsletter submit link also redirects to https://example.com/category/news
How to avoid it?

I don't know how you are currently issuing the redirect, but in order to target the query string (in order to exclude that particular query string) then you'll need to use mod_rewrite.
For example:
RewriteEngine On
RewriteCond %{QUERY_STRING} !=na=s
RewriteRule ^$ /category/news [QSD,R=301,L]
The above redirects the homepage (ie. an empty URL-path), but specifically excludes the URL with the exact query string na=s.
The QSD flag discards any other query string that might have been on the initial request.
You will need to clear your browser cache before testing (since the erroneous 301 permanent redirect will have been cached by the browser). Test first with a 302 (temporary) redirect to avoid caching issues.
So after basic 301 redirection, newsletter submit link also redirects to https://example.com/category/news
Although, ordinarily you would expect it to have redirected to https://example.com/category/news?na=s, unless you had taken explicit measures to remove the query string from the redirect response.

Related

How do I redirect just my home page and keep rest of the URLs as it is?

I need to redirect some URLs as they are pointing to same page. For example:
https://www.example.com/home
https://www.example.com/Home
https://www.example.com/index.php
These all URLs need to refirect to:
https://www.example.com/
So I am using this in .htaccess file it is working fine :
Redirect 301 /home https://www.example.com/
But the issue is when I try to access the URL like this:
https://www.example.com/home/sub
It redirects to resulting in 404:
https://www.example.com//sub
I need this URL to work as it is instead of 404 or any redirect:
https://www.example.com/home/sub
Redirect 301 /home https://www.example.com/
Because the mod_alias Redirect directive is prefix-matching and everything after the match is copied onto the end of the target URL. So, when you request /home/sub, /sub is copied on to https://www.example.com/, resulting in https://www.example.com//sub.
I imagine you also have mod_rewrite directives. You should avoid mixing redirects from both modules, so try the following instead using mod_rewrite at the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(home|index\.php)$ / [R=301,L]
The condition that checks against the REDIRECT_STATUS environment variable ensures that only direct requests are processed, as opposed to internally rewritten requests which will result from having a front-controller pattern (which I assume you have later in the file).
You will need to clear the browser cache since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302s to avoid potential caching issues.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

How add path to url if the url does not contain substring

I need to redirect all requests from
https://example.com/* to https://example.com/test/* if the URL does not contain the test substring.
So far I have these rewrite rules
RewriteBase /
RewriteCond %{THE_REQUEST} !^/test
RewriteRule ^/?$ /test/$1 [R=301,L] # if the url does not contain test, redirect to url with test
RewriteCond %{THE_REQUEST}% test
RewriteRule ^test?(.*)$ /$1 [L] # mask the fact that the url is not https://example.com/ and instead is https://example.com/test but apache serve the website like if it was on root
If I access https://example.com it redirects to https://example.com/test but gives infinite loop because of the second rule.
How can I combine it, so request to https://example.com/test* do not get redirected but those request at https://example.com/* do without having to change www root directory and so it will work for all URLs.
UPDATE:
The test should be in url (for user experience), but the apache should route like if it was not in url and instead the request came to root url, so application routing is preserved internally without having to change the app itself.
Ah, Ok. However, you should be linking to the /test URLs within your app (so you do still need to "change the app", despite your last comment), otherwise /test isn't actually in the URLs that users and search engines see on the page (they will be redirected) and your users will experience an external redirect every time they click one of your links (bad for SEO and user experience).
The "redirect" implemented in .htaccess to prefix "old" URLs with /test is just for SEO - as with any "old" to "new" URL change. (It should not be required for your app to function - with /test in the URL-path - since your internal URLs should already include /test.)
Try it like this instead:
RewriteEngine On
# Insert "/test" at the start of the URL-path if absent in "direct" requests
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^test/ /test/$1 [R=301,L]
# Rewrite "/test" URLs back to root
RewriteRule ^test(?:$|/(.*)) /$1 [L]
The REDIRECT_STATUS environment variable is used to prevent a redirect loop. This is empty on the initial request from the client and set to the HTTP response status after the rewrite (below).
Test first with 302 (temporary) redirects and only change to 301 (permanent) when you are sure this is working as intended.
You will need to clear your browser cache before testing.

.htaccess redirect link to same url but adding query string

I'm trying to use pretty affiliate urls based on the username of my website users.
I need to redirect a link like:
https://example.com/anything-here/username
To
https://example.com/anything-here/?ref=user_id
I've found this solution:
RewriteRule ^(.*)username(.*)$ https://example.com/?ref=user_id [L,R=301]
It works, but redirects to the page I set here (in this case, it's the home page). My question is:
How can I redirect it to the page requested at the beginning, but removing the username and adding /?ref=user_id?
You look as if you are close, you just need to make use of the backreferences. Try the following:
RewriteRule ^(.*)/username$ https://example.com/$1/?ref=user_id [R=302,L]
The $1 is a backreference to the first captured group in the RewriteRule pattern.
Also, you don't appear to need the (.*) at the end of the pattern, since username is at the end of the URL (at least in your example).
This is a 302 (temporary) redirect - change it to 301 (permanent) if this is required.
You will need to clear your browser cache before testing.

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

301 redirect urls with a slash in querystring

We built a new webshop for one of our clients, and are 301 redirecting their old url's to our normal ones. As usual we do this using .htaccess as follows:
Redirect 301 /url1/ http://www.url2.com/ and it works fine.
BUT the old shop has this querystring with slashes in it (!) , for example:
/epages/14353.sf/de_DE/?ObjectPath=/Shops/61922345/Products/32428
And as soon as I use this in a htaccess 301 redirect string it stops working. I don't get a 500 error or something like that, but when I visit /epages/14353.sf/de_DE/?ObjectPath=/Shops/61922345/Products/32428 it won't redirect. If I would use it with a querystring like the following (/epages/14353.sf/de_DE/?ObjectPath=foobar) I can visit it and get redirected, but not if there's a slash in it.
I tried backslashing it, encoding etc. but without the right results. Does any1 have an idea? I tried AllowEncodedSlashes On but it gave me a 500 error.
Since you cannot match QUERY_STRING in Redirect directive, you need to use mod_rewrite based rule.
Have this one in your root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ObjectPath=/Shops/61922345/Products/32428
RewriteRule ^/?epages/14353\.sf/de_DE/?$ https://www.nu.nl? [L,NC,R=301]
Make sure to test this in a new browser to avoid old cached data.