How can I redirect subdomain.domain.com/* to domain.com/folder/* - .htaccess

I have to make a redirection on a .htaccess file.
I want to access the content of a folder from a subdomain url like this:
subdomain.domain.com/* => domain.com/folder/*
The folder contains pdf files and I want to acces it with this url for example:
subdomain.domain.com/file.pdf
I'm new into htaccess redirection rules and I'm a little lost.
I tried something like this and test it into https://htaccess.madewithlove.com/
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
This code works on the tester but on my website it throws me the error : "The connection was reset".
Do you have any idea on it?
UPDATE
Following some advices I try but it doesn't work
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com/
RewriteRule (.+\.pdf)$ https://example.com/folder/$1 [R=302,L]

RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
You are not doing anything with the captured URL-path (ie. (.*)) so this will always redirect to https://domain/folder/ (no file).
I would also question whether this should be a 301 (permanent) redirect. Maybe a 302 (temporary) redirect would be preferable here? Note that the 301 is cached, so you will need to clear your browser cache before testing.
It should be like this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule (.*) https://domain/folder/$1 [R=302,L]
The $1 backreference contains the URL-path captured in the RewriteRule pattern.
The Host header (ie. the value of the HTTP_HOST server variable) contains the hostname only. There is no URL-path component. (Fortunately /* matches the slash 0 or more times, so it still "worked" in your testing.)
However, if the folder only contains .pdf files then you should be more restrictive and redirect only .pdf requests. For example:
:
RewriteRule (.+\.pdf)$ https://domain/folder/$1 [R=302,L]

Related

htaccess redirect with query string not working

enter code hereI have a WordPress website.
I have URLs for affiliates that look like this:
https://example.com/folder/?ref=23432
https://example.com/folder/?ref=13442
etc.
I would like to redirect any URL that ends in ?ref= to another domain.
For example, https://example.com/folder/?ref= should redirect to https://example.org/product/
How can I do this? I appreciate your time.
I tried
Redirect 301 example.com/folder/?ref https://example.org/product/
Thank you #MrWhite. I tried the following with no success.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
The RewriteRule directive matches the URL-path only (less the slash prefix). So this should be matching against folder/ (as per your example), not the hostname.
And the $0 backreference in the substitution string is not required here. So this should simply be:
:
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
If you do need to check the requested hostname (ie. example.com) - if example.com and example.org point to the same server - then you need a separate condition (RewriteCond directive). For example, the complete rule would then become:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
Note that the regex (^|&)ref= matches the ref= URL parameter anywhere in the query string, if there happened to be other URL parameters that preceded it.
Reference:
htaccess redirect URL with parameter when a special parameter exists
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

How can I redirect match full subdomain urls, but make the root subdomain go elsewhere using htaccess

I have a bunch of old urls from a subdomain I need redirected to another subdomain and retain the url pattern. The issue I'm struggling with is getting just the root subdomain to redirect to another location.
For context, here is an example of the old url structure:
https://oldsub.maindomain.com/2015/07/30/url-title/
Which I need redirected to:
https://blog.maindomain.com/blog/2015/07/30/url-title/
Notice it needs to be directed to a new blog subdomain and there is a blog slug added after the main domain.
If anyone visits just the root of the old subdomain (https://oldsub.maindomain.com/) I need that redirected to a URL structure on just the main domain (https://maindomain.com/specific-url-title/)
I have my .htaccess within the root of the subdomain directory.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsub\. [NC]
RewriteCond %{HTTP_HOST} ^(?:oldsub\.)?(.+)$ [NC]
RewriteRule ^ https://blog.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^oldsub\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/specific-url-title/ [L,NE,R=301]
In the .htaccess of the root domain I have:
RewriteCond %{HTTP_HOST}
RedirectMatch 301 ^/blog/(.*)$ https://blog.maindomain.com/blog/$1
So the old blog posts redirect properly to the blog.maindomain.com/blog/rest-of-url but the last two lines aren't working as the old subdomain request simply goes to blog.maindomain.com/blog as well.
The file structure is:
/public_html (maindomain.com)
.htaccess
/oldsub.maindomain.com (oldsub.maindomain.com)
.htaccess
blog.maindomain.com lives on another server (on hubspot platform)
Your current redirects are not correct, so these need to be fixed. (Although you should have seen a redirect of some kind.)
RewriteCond %{HTTP_HOST} ^oldsub\. [NC]
RewriteCond %{HTTP_HOST} ^(?:oldsub\.)?(.+)$ [NC]
RewriteRule ^ https://blog.%1%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^oldsub\.gppcpa\.com$ [NC]
RewriteRule ^ https://maindomain.com/specific-url-title/ [L,NE,R=301]
The problems with these directives:
The first rule does not redirect to the /blog subdirectory.
These rules conflict. The first rule will also redirect the document root of the subdomain. Consequently, the second rule will never be processed.
Since the subdomain in question is completely outside of your maindomain, in terms of where these hostnames point to on your filesystem, then there is no need to check the hostname in these directives since only the oldsub subdomain can access this area.
Try the following instead:
RewriteRule . https://blog.maindomain.com/blog%{REQUEST_URI} [R=302,L]
RewriteRule ^ https://maindomain.com/specific-url-title/ [R=302,L]
The single dot in the first RewriteRule pattern matches "something" (not "nothing"). When the URL-path is empty (ie. the document root) this falls through to the second directive.
Make sure you clear your browser cache before testing.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure it's working OK. This is to avoid any caching issues, since 301s are cached persistently by the browser (including those made in error).
RewriteCond %{HTTP_HOST}
RedirectMatch 301 ^/blog/(.*)$ https://blog.maindomain.com/blog/$1
From your maindomain .htaccess file... That RewriteCond directive is erroneous here and should be removed.
To avoid potential conflicts with mod_rewrite you should change the mod_alias RedirectMatch directive to mod_rewrite. For example:
RewriteEngine On
RewriteRule ^blog/(.*) https://blog.maindomain.com/blog/$1 [R=302,L]
Different Apache modules (mod_rewrite, mod_alias, etc) run at different times throughout the request and run independently of each other. mod_rewrite usually runs first (regardless of the apparent order of the directives in your config file), but this also means that mod_alias (Redirect, RedirectMatch) will always run, even though an earlier mod_rewrite directive (RewriteRule) might have rewritten the URL and stopped execution.

htaccess to change only URL address

I have a site and need htaccess code to make this change in the address bar without making any physical changes in the server folders
example.com/data/65767.html
to be
example.com/65767
example.com/data/56-45.html
to be
example.com/56-45
Regards
Horribly inefficient, you need to make sure your links get changed to point to the one without .html, but...
RewriteEngine On
# externally redirect so that the browser shows the non-.html URL
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /data/([0-9-]+)\.html
RewriteRule ^ /%2 [L,R=301]
# internally rewrite the non-.html URI back to the .html URI
RewriteCond %{DOCUMENT_ROOT}/data%{REQUEST_URI}.html -f
RewriteRule ^/?([0-9-]+)$ /data/$1.html [L]
This works with /data/<any number of digits or "-"> URIs. If you want it to work with anything, replace the [0-9-] with .
EDIT:
can we also redirect example.com/65767.html to be example.com/65767 and the content from example.com/data/65767.html
Yeah, just change the first rule to:
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /(data/)?([0-9-]+)\.html
RewriteRule ^ /%3 [L,R=301]

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

301 Redirects problem - urls with ? and =

I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113&param=value will not be redirected because query string is group=113&param=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]

Resources