.htaccess redirect to subdomain with regex - .htaccess

I want to 301 redirect a specific path to another subdomain with htaccess
I want :
A : http://www.example.org/markets/access/id/56
B : http://www.example.org/markets/market/id/56
to redirect to :
A : https://archive.example.org/markets/access/id/56
B : https://archive.example.org/markets/market/id/56
I have tried this but get an error 500:
RewriteCond http://www.example.org/markets/(.+?)
RewriteRule https://archive.example.org/markets/$1 [R=301,L]
Thank you

Based on your shown samples, could you please try following. Please make sure you put these rules at the top of your .htaccess file.
Also make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(example\.org)$ [NC]
RewriteRule ^ https://archive.%1%{REQUEST_URI} [NE,R=301,L]

Related

htaccess url redirects with url contains = something

I want to create some userfriendly new redirects for my website.
My internal search pattern is the following : website.com/search/stuff
I want to add in my htaccess a rule that would redirect : /search=stuff
to : /search/stuff
I was trying the following code into my htaccess file :
RewriteCond %{QUERY_STRING} \\search=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
But that doesnt seem to work... any ideas of where I may be wrong?
Thanks !

How to get /folder/subfolder to display as /subfolder/ using htaccess

I'm trying to do the following...
Original path that needs to be rewritten, renamed, or redirected:
http://www.example.com/_plugin/notifications/
to:
http://www.example.com/notifications/
via root htaccess file...Any help would be greatly appreciated!
=================================================================
This does not work:
RewriteEngine On
RewriteRule ^/_plugin/notifications$ /notifications [L]
This does not work either:
RewriteEngine On
RedirectMatch ^/_plugin/notifications$ /notifications/
This does not work either:
RewriteEngine On
RewriteRule ^notifications/(.*)$ /notifications$1 [R=301,NC,L]
=============================
EDIT
With the help of #Starkeen - I have a few follow up questions to this.
Is there a way to shorten the .htaccess file up though if lets say I
have multiple subfolders within the folder instead of writing that 1
condition and the 2 rules for each subfolder?
How would I allow subfolders of the subfolder to display? Currently it is throwing a 404 at me... :(
============
Still need help with follow-up question #1, but I believe I got question #2.
SOLUTION (I believe) to follow-up question #2:
RewriteCond %{THE_REQUEST} /_plugin/notifications/ [NC]
RewriteRule ^_plugin/notifications/(.*)$ /notifications/$1 [L,R]
RewriteRule ^notifications/(.*)$ /_plugin/notifications/$1 [L]
None of the rules you have tried are correct.
To redirect /folder/subfolder to /root/subfolder you need a permanent 301 Redirect rule something like the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} /folder/subfolder/ [NC]
RewriteRule ^folder/subfolder/$ /subfolder/ [L,R]
The above will redirect http://example.com/folder/subfolder/ to http://example.com/subfolder .
You will get a 404 error if the /subfolder/ doesn't exist in the root dir. To avoid the 404 error you can rewrite the /subfolder/ uri back to its original location /folder/subfolder/ using an internal Rewrite just bellow the first one
:
RewriteRule ^subfolder/?$ /folder/subfolder/ [L]

.htaccess 301 redirect to new domain by editing path

I need to redirect old domain to new with .htaccess
Situation:
www.oldodmain.com/en/categoryA/product1
www.newdomain.com/en/categoryB
Result I am trying to achieve
www.newdomain.com/en/categoryB/categoryA/product1
Tried to do with this code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?voniospasaulis\.lt$ [NC]
RewriteRule ^.+$ http://www.visaslabas.lt/lt/vonios-iranga/%{REQUEST_URI} [L,R=301]
But I get a result as follow:
www.newdomain.com/en/categoryB/en/categoryA/product1
I need to get rid of /en/before/categoryA to get url like this:
www.newdomain.com/en/categoryB/categoryA/product1
Your rule pattern has to start with en/ and should capture value after en/ in $1 that you can use in target:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldodmain\.com$ [NC]
RewriteRule ^en/(.+)$ http://www.newdomain.com/en/categoryB/$1 [L,NE,NC,R=301]
Also note that I have answered using dummy domain names instead of your actual domain names shown in question.
Make sure to clear your browser cache or use a new browser for testing.

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

URL Rewrite Conditions & Parameters

----EDIT---
I have just realized that my explanation of the problem was missing an important piece of information.
The URL's should only be redirected if second parameter is present.
So the rule should read:
Redirect any URL that has /d/ in it, ONLY if /d2/ is also found in the URL.
----End Edit__
I have the need to 301 redirect all URL's on a site that contain a specific parameter to the same URL, but with an additional directory included. All of the URL's that require redirection contain a certain directory: /d/ Example:
http://www.mysite.com/category1/d/subcategory1/subdirectory2/
--Should Redirect to --
http://www.mysite.com/newdirectory/category1/d/subcategory1/subdirectory2/
The one thing in common with any of the URLs' requiring redirection is that they all contain a directory /d/ in the URL, which always immediately follows the "category" directory as indicated in bold in sample URL's above. I would then like to insert an additional directory in front of the category directory as indicated in bold in the sample URL's above. The rest of the URL will remain the same.
Can anyone help with this? I'm relatively new to mod_rewrite and realize I can make a big mess if I don't get it right.
Thanks in advance for anyone who can offer hlep
:)
Try this (edited to reflect change in question):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*/d/.* [NC]
RewriteCond %{REQUEST_URI} ^.*/d2/.* [NC]
RewriteCond %{REQUEST_URI} !/newdirectory/ [NC]
RewriteRule ^(.*)/d/(.*)$ http://www.mysite.com/newdirectory/$1/d/$2 [R=301,L]
Try adding the following to your htaccess file in the root directory of your site.
RewriteEngine On
RewriteBase /
#if the url contains a /d2/ [NC]
RewriteCond %{REQUEST_URI} /d2/ [NC]
RewriteRule ^([-a-zA-Z0-9]+/d/[-a-zA-Z0-9]+/[-a-zA-Z0-9]+/)$ /newdirectory/$1 [L,R,NC]

Resources