htaccess rewrite single + multiple URLs - .htaccess

I need to do some redirects from one domain to the other: a number of specific redirects + a general rule:
Specific redirects:
subdomain.example.com/ => example.com/subdomain/
subdomain.example.com/page1 => example.com/subdomain/page1
General rule - should apply to all URLs not captured with the above rules:
subdomain.example.com/* => example.com/*
Here are the current rules I have - the problem is that they seem to be mutually exclusive:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule /? http://example.com/subdomain/ [R=301,L]
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
What am I doing wrong

You have incorrect RewriteRule pattern, you should enclose /? with ^/?$, otherwise it matches all URL.
^: start of line
$: end of line
# first specific rule
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^/?$ http://example.com/subdomain/ [R=301,L]
# second specific rule
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^page1$ http://example.com/subdomain/page1 [R=301,L]

Related

Problem HTACCESS: How to give priority absolute path?

We are busy with a name change for our webshop and i am working on our HTACCESS to redirect 1700 links. from those 1700 links there are 177 links that are changing in our new webshop. So they have to be in the HTACCESS. The other links keep the same and i redirect them now with a general rewriterule.
The only problem now is that he does not look well what the exact link is. For example see below my HTACCESS.
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/(.*)$ https://www.new.nl/vloerkleden/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/vintage-vloerkleed/(.*)$ https://www.new.nl/vloerkleden/vintage-vloerkleed/ [R=301,L]
RewriteCond %{HTTP_HOST} ^old.nl [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.nl [NC]
RewriteRule ^(.*)$ https://www.new.nl/$1 [L,R=301,NC]
When i type now the following url in the browser www.old.nl/vloerkleden/catagorie/vintage-vloerkleed/ he links me to www.new.nl/vloerkleden in stead of www.new.nl/vloerkleden/vintage-vloerkleed.
Your problem in this line :
RewriteRule ^vloerkleden/categorie/(.*)$ https://www.new.nl/vloerkleden/ [R=301,L]
Nothig to present (.*)$ in substitution https://www.new.nl/vloerkleden/ so , it should look like this https://www.new.nl/vloerkleden/$1 because $1 will represent (.*) in pattern.
Also , you could do this with another rules and you could also sumerize your rules like this :
RewriteEngine on
# the folwoing rules will force every request for both old & new into https://wwww:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(?!.*www\.)(.*)$
RewriteRule .* https://www.%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/(.*)$ https://www.new.nl/vloerkleden/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/vintage-vloerkleed/(.*)$ https://www.new.nl/vloerkleden/vintage-vloerkleed/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old.nl [NC]
RewriteRule ^(.*)$ https://www.new.nl/$1 [L,R=301,NC]
Note: clear browser cache then test.

Multiple redirects with 301 htaccess

I am new to redirects and regex. I have an issue with redirects implemented. Here is the example. I want
http://www.example.com/support redirect to
https://www.example.com/support/
But here is what happens
http://www.example.com/support redirects to
https://www.example.com/support redirects to
https://www.example.com/support/
Below is the htaccess contents for it
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# To remove double slash in the middle
RedirectMatch 301 ^/(.*)//(.*)$ https://www.example.com/$1/$2/
# redirect index.php to root
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)/index\.php$ https://www.example.com/$1 [R=301]
#index to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.example.com/$1 [R=301,L]
You can use these rules t avoid multiple redirecects:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# To remove double slash anywhere
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=302,L,NE]
# redirect index.php to parent
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
Make sure to use a new browser for testing.
#anubhava your edit shows the attachment 1
The existing code in the question I posted does below
However, I am looking for a single redirect to the final destination..
Recent update by adding trailing slash results are below

How to fix htaccess RewriteRule back reference to RewriteCond? Fix for CPANEL AutoSSL Let’s Encrypt

CPANEL with AutoSSL (Let’s Encrypt) add automatic RewriteCond to htaccess because .well-known folder and keep updating htaccess file every X minutes, so you can't delete the "well-know" lines. But with this approach they break RewriteRule back reference to RewriteCond.
# www to non-www generic
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This example above don't work as spectated because %1 references last RewriteCond, and not the first where we filter the host with regexp.
Some fix for generic www to non-www, using variables
# www to non-www (fixed to work with CPANEL - AutoSSL)
SetEnvIf Host "^www\.(.*)$" my_domain=$1
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://%{ENV:my_domain}/$1 [R=301,L]
To bypass this problem use variable, it's not pretty but works!

htaccess rewrite - pulling variable from dynamic host

I have a set of mod-rewrite rules which look like this:
RewriteCond %{HTTP_HOST} AAAexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.AAAexample.com/folder/AAA [R=301,L]
RewriteCond %{HTTP_HOST} BB-BBexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.BB-BBexample.com/folder/BB-BB [R=301,L]
RewriteCond %{HTTP_HOST} CCCCCexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.CCCCCexample.com/folder/CCCCC [R=301,L]
Essentially I have a set of domains matching (PATTERN)example.com, where (PATTERN) can contain any combination of letters and hyphens.
Is there a way to condense the above rules into a single set such that I Pull PATTERN as a dynamic variable into the final rewrite URL?
Thanks!
You can use:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)(example\.com)$ [NC]
RewriteRule ^/?$ http://www.%1%2/folder/%1 [R=301,L]

.htaccess conditional statement

I want to rewrite my urls to more seo urls using wildcard sub-domains and I am unable to write htaccess conditions and hoping to get some help with it.
I have this url:
http://learntipsandtricks.com/blog/c/magento
I want to rewrite as:
http://magento.learntipsandtricks.com/
Change pagination link:
http://learntipsandtricks.com/blog/92/magento/3
to:
http://magento.learntipsandtricks.com/p-92-3
Finally change article url:
http://learntipsandtricks.com/blog/magento/114/magento-index-management-Cannot-initialize-the-indexer-process
to:
http://magento.learntipsandtricks.com/114-magento-index-management-Cannot-initialize-the-indexer-process
I wrote this:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/p\-(\d+)\-(d+) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/(\d+)\-(.*) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/%2/%3/$1 [P]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/c/%1/$1 [P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
The above code doesn't work. Is it possible using if else in htaccess to check all types of urls and rewrite accordingly?
EDIT:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/[first part of sub-domain]/%2/$1 [P]
How can I get first part of sub-domain here.
Thank you.
HTTP_HOST doesn't include the URL-path. If you want to match against the URL-path, use REQUEST_URI instead
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$
for example.
From RewriteCond Directive
RewriteCond backreferences: These are backreferences of the form %N (0 <= N <= 9). %1 to %9 provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions. %0 provides access to the whole string matched by that pattern.
Therefore, if you want to capture both the domain and URL-path components, you must combine HTTP_HOST and REQUEST_URI in one RewriteCond
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(.+)\.learntipsandtricks\.com/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]

Resources