htaccess redirect with variable path match - .htaccess

I want to redirect from example.com to example2.com where the first path segment is a variable:
I.e. to redirect from:
example.com/aariz/name/meaning/english
example.com/arif/name/meaning/english
example.com/adil/name/meaning/english
example.com/ayat/name/meaning/english
To:
example2.com/aariz/name/meaning/english
example2.com/arif/name/meaning/english
example2.com/adil/name/meaning/english
example2.com/ayat/name/meaning/english
This is the code I have:
RewriteCond %{REQUEST_URI} ^(.*)/name/ [NC]
RewriteRule /(.*) https://example2.com/$1 [R=301,L]
But in all cases it's redirecting to example2/name/meaning/English - the first path segment is missing.
How can I redirect maintaining the whole path?

Related

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

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]

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.

RewriteCond if url is not "cdn.domain.com/images/"

we have created a CDN subdomain to host images, using this URL:
https://cdn.example.com/images/
What we want is to redirect, if someone goes to any other path of this subdomain. For example:
https://cdn.example.com/
https://cdn.example.com/blabla/
Redirect to other domain, just not redirect if the folder is images.
create htaccess file in your document root, and deny direct access to all files
Order deny,allow
Deny from all
and create htaccess file in images:
Allow from all
Try the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn.example.com [NC]
RewriteRule !^images/ https://example.com%{REQUEST_URI} [R,L]
If you access cdn.example.com/<something> and the URL-path does not start /images/ then redirect to https://example.com/<something> (the main domain).
Change R to R=301 (after you've made sure it is working OK) if you want a permanent redirect.
UPDATE: To exclude additional folders (eg. images2 and images3) then you can change the RewriteRule directive to:
RewriteRule !^(images|images2|images3)/ https://example.com%{REQUEST_URI} [R,L]
If the folders are literally called images, images2 and images3 then this could be simplified to match (or rather not match) any URL of the form /imagesN - where N is an optional digit:
RewriteRule !^images\d?/ https://example.com%{REQUEST_URI} [R,L]

Redirect subfolder if folder name is not 'blogparty' and prefix starts with 'blog'

I want a redirect to happen if subfolder prefix is 'blog' and 'X' is not equal to 'party'.
This should not redirect
http://www.hostname.co.uk/blogs/blogparty
This should redirect
http://www.hostname.co.uk/blogs/blogX
to
http://www.hostname.co.uk/blogs/X
where 'X' is any string which does not equal to 'party'.
This should work on sub directories of this directory as well meaning this should be redirected too.
http://www.hostname.co.uk/blogs/blogX/blah/index.php
to
http://www.hostname.co.uk/blogs/X/blah/index.php
This is what i tried which is redirecting everything including 'blogparty'
# Redirect blogBLOGNAME to BLOGNAME excluding blogparty
RewriteCond %{REQUEST_URI} !^/blogs/blogparty/.*
RewriteCond %{REQUEST_URI} !^/blogs/blogparty$
RewriteRule ^/blogs/blog(.*) /blogs/$1 [R=301,L]
Your rules should have worked. I believe there are other routing rules below your shown rule. To handle this you can have your rule based on THE_REQUEST instead (which doesn't change after other rules):
RewriteCond %{THE_REQUEST} !\s/+blogs/blogparty(/\S*)?\s [NC]
RewriteRule ^(blogs)/blog(.+)$ /$1/$2 [R=302,L,NC,NE]

Resources