Redirect domain and page ID - regex

I would like to redirect domain and product ID using 2 variable the first one for domain and second one for page id like:
RewriteRule ^/(.*)$/4640-(.*)$ https://www.new-domain.com/$1/5291-$2 [R=301,L]
Example from:
https://www.old-domain.com/folder_A/4640-product_name.html
to
https://www.new-domain.com/folder_A/5291-product_name.html

Regex for RewriteRule shouldn't start with /:
RewriteRule ^(.*)$/4640-(.*)$ https://www.new-domain.com/$1/5291-$2 [R=301,L]
And #Wiktor Stribiżew is right:
You have two end of string anchors in the regex. Use one or none - ^/(.*)/4640-(.*)
Fix it with:
RewriteRule ^(.*)/4640-(.*)$ https://www.new-domain.com/$1/5291-$2 [R=301,L]

Related

regex with query string containing?

I am trying to redirect the following URL:
url/efx.aspx?xxxxxxx
to url/car-audio/efx-hardware/amp-install-kits
However it is redirecting whatever contains efx.aspx with the letters without the ? sign. I was wondering how I can fix this?
for example it is redirecting the following:
domain.com/efx.aspxlsdkjfhlasdf
but it is not redirecting
domain.com/efx.aspx?lsdkjfhlasdf
here is the .htaccess rule I wrote. how can I correct it?
RewriteCond %{REQUEST_URI} /efx.aspx[^/]+$
RewriteRule (.*) /car-audio/efx-hardware/amp-install-kits [R,L]
You can use this rule:
RewriteRule ^efx\.aspx$ /car-audio/efx-hardware/amp-install-kits? [R=301,NC,L]
Query string is not part of REQUEST_URI hence [^/]+ after efx.aspx fails your rule.
Also ? at the end of target URI removes any existing query string.

htaccess RewriteRule for found id in middle of url

I need get objectid from seo url e.g When I use this rule:
RewriteRule ^company/([A-Za-z0-9-]+)$ /lt?openobjectid=$1
evrything is ok when I use example.com/company/12 but if use example.com/company/12/some-text I get server error.
It is because your regex pattern is not matching example.com/company/12/some-text.
You can try this rule:
RewriteRule ^company/([a-z0-9-]+)(?:/.*)?$ lt?openobjectid=$1 [L,QSA,NC]

Apache mod_rewrite (RewriteCond) to filter out the "in-between" word?

(I'm not very used to RewriteCond things. So that i just googled and used for existing ones.)
Now I have a piece of .htaccess codes which rewrites into a different URL upon the input:
RewriteCond %{HTTP_HOST} ^(.*).example.com$
RewriteRule ^(.*)$ http://www.example.com/%1/$1 [P,L,NS]
By using that, it will rewrite by taking the "sub domain" input, and then putting as a folder, as in output. Lets say:
Input: http://support.example.com
Output: http://www.example.com/support
Input: http://member.example.com
Output: http://www.example.com/member
So that is working.
Now what i need is a bit more complicated out of this existing one.
How to filter the "in between" words by the RewriteCond?
Lets say i have 2 levels of sub-domains down. And then i ONLY want to select/filter the upper one. Which means:
Input: http://dev.support.example.com
Output: http://dev.example.com/support
(Inputs can also be: dev.member, dev.pricing, etc alot)
How to filter that support word, out of dev.support string?
Current ^(.*).example.com$ is only for the far left item.
Below one is NOT working:
RewriteCond %{HTTP_HOST} dev.^(.*).example.com$
RewriteRule ^(.*)$ http://dev.example.com/%1/$1 [P,L,NS]
Please suggest.
In your regex ^ needs to be placed at start of your pattern and use [^.]+ is better pattern than .*
You can use:
RewriteCond %{HTTP_HOST} ^dev\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://dev.example.com/%1/$1 [P,L,NS]

regex to redirect non www to www and subdomain to file

I need to do the following .. have come across various examples but i need to combine three conditions
redirect
1) redirect non www / non subdomain requests. eg :
http://xyzsite.com to http://www.xyzsite.com
2) redirect if subdomain is mentioned . eg :
http://user1.xyzsite.com to http://www.xyzsite.com/profile?user1
3) redirect to mobile version. eg :
http://m.xyzsite.com to http://www.xyzsite.com/m
Tech details :
I m on IIS ver 6 & using helicontech isapi_rewrite module
1.
Match: ^xyzsite.com$
Redirect: www.xyzsite.com
2.
Match: ^(?!www.)(.*).xyzsite.com$
Redirect: www.xyzsite.com/profile?$1
3.
Match: ^m.(.*)$
Redirect: www.$1/m
Here are the ISAPI_Rewrite v3 rules (hope this is the version you use):
RewriteBase /
RewriteCond %{HTTP_HOST} ^xyzsite\.com$
RewriteRule .? http://www.xyzsite.com [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^m\.xyzsite\.com$
RewriteRule .? http://www.xyzsite.com/m [NC,R=301,L]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.xyzsite\.com$
RewriteRule .? http://www.xyzsite.com/profile?%1 [NC,R=301,L]
I spent some time on this to hopefuly get you in the right direction. I come up with easiest solution unless you specified a constraint expliciterly.
This means that I hardcode xyzsite.com in the regex. This actualy higlight the essense more of the solution
1) redirect non www / non subdomain requests. eg : http://xyzsite.com to http://www.xyzsite.com
pattern:
http://(.*?.com)
replacement:
http://www.$1
2) redirect if subdomain is mentioned . eg : http://user1.xyzsite.com to http://www.xyzsite.com/profile?user1
pattern:
(http://)(.*?)\.(.*)
replacement:
$1www.$3/profile?$2
3) redirect to mobile version. eg : http://m.xyzsite.com to http://www.xyzsite.com/m
pattern:
http://m\.(.*)
replacement:
http://www.$1/m
Hope this helps, Buckley

How to rewrite ALL urls while skipping certain folders?

i'm trying to rewrite my urls to goto a single php file:
RewriteRule ^dir/(?:(?!style|js).)*$ http://www.domain.com/single.php?uri=$1 [QSA]
However the exclusion of /dir/js and /dir/style isn't working as i was hoping it would...
[redirects] domain.com/dir
[redirects] domain.com/dir/jason
[redirects] domain.com/dir/jason/pete
[DOESN'T REDIRECT: GOOD] domain.com/dir/js
[DOESN'T REDIRECT: GOOD] domain.com/dir/js/*
[DOESN'T REDIRECT: BAD] domain.com/dir/json
How can I change the regular expression to match my needs?
Try to replace style|js with style\b|js\b.
Maybe RewriteCond could be of use like in
RewriteCond %{REQUEST_URI} !^/dir/(style|js)($|/)
RewriteRule ^/dir/(.*)$ http://www.domain.com/single.php?uri=$1 [QSA]
EDITED:
domain.com/dir/json doesn't redirect because it doesn't match the regex.
The reason /dir/json doesn't redirect is because js follows dir/, and your regex only matches when dir/ is not followed by either style or js. I think negative lookaheads are the wrong approach. I think what you actually want is something like:
RewriteCond %{REQUEST_URI} !^/dir/(js|style)(/.*)?$
RewriteRule ^dir/(.*)$ http://www.domain.com/single.php?uri=$1 [LQSA]
That basically means if the URL isn't ended with /js or /style (optionally with further path components underneath those dirs), then apply the redirect rule.
Either with your negative look-ahead assertion:
RewriteRule ^dir/(?!(?:style|js)(?:/|$))(.*) http://www.example.com/single.php?uri=$1 [QSA]
But that’s not so nice.
Or you just add another test on how $1 starts:
RewriteCond $1 !^(style|js)(/|$)
RewriteRule ^dir/(.*) http://www.example.com/single.php?uri=$1 [QSA]