Moving using 301 in htaccess using GET variables - .htaccess

What am I doing wrong?
I have
http://www.site.com/shop/viewmy/?ASIN=234FhK3
http://www.site.com/shop/viewmy/?ASIN=423ZH3
Trying to convert to
http://www.site.com/shopping/234FhK3/moved
http://www.site.com/shopping/423ZH3/moved
I'm going to change the "moved" part with an internal redirect after I get some info about the product 234FhK3 , a variable that changes.
I have tried
RewriteCond %{QUERY_STRING} ASIN=([^&]+) [NC]
RewriteRule ^[^/]+/(shopping/)/?$ $1.(/moved)? [L,R=301,NC]
I'm not too good with htaccess rewrites.

Try this:
RewriteCond %{QUERY_STRING} ASIN=([^&]+) [NC]
RewriteRule ^shop/viewmy/$ /shopping/%1/moved/? [L,R=301,NC]

Related

How can i redirect /?lang=en|ru to /?lang=en#googtrans(en|ru)?

How can I add the Google translate parameter #googtrans(en|de) or other language, so the translation happens automatically?
Basically, when the user goes to https://example.com/page/?lang=de they are redirected to https://example.com/page/?lang=en#googtrans(en|de)
I use this .htaccess rule, but it's not working:
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^/?lang=en#googtrans(en|[a-z]{2}) [R=301,L]
EDIT: Adding edited Rules here.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/page/?\?lang=([a-z]{2})\s [NC]
RewriteRule ^ page/?lang=%1#googtrans(%1) [R=301,L,NE]
With your shown samples(this is considering that you are hitting URL like: https://example.com/page/?lang=de in browser), please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$ [NC]
RewriteRule ^page/?$ page/?lang=%1#googtrans(%1) [R=301,L,NE]

.htaccess language subdomain redirect

I am trying to redirect my language variables domain.com?lang=X to subdomains X.domain.com
I have this code:
RewriteCond %{QUERY_STRING} (^|&)lang=(es|de|fi|tr)
RewriteRule ^(.*)$ https://%2.domain.com%{REQUEST_URI} [R=301,L]
Unfortunately, when I want to change language by going to domain.com/page?lang=es, I'm redirected to es.domain.com/page?lang=es which causes a loop.
So I tried adding a questionmark to the end of the URL like so:
RewriteRule ^(.*)$ https://%2.domain.com%{REQUEST_URI}? [R=301,L]
to prevent it from adding the ?lang= variable when accessed by the subdomain but in that case the language doesn't change and I end up with es.domain.com/page in English.
I've spent the last 4 hours digging through StackOverflow and other sites trying to find a solution but to no avail. How can I detect that I'm already redirected to the subdomain and then ignore the ?lang= variable while actually changing the language?
Please help.
Thank you.
You will need 2 rules for this:
RewriteEngine On
# external redirect rule using THE_REQUEST variable
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+([^?]*)\?&?lang=(es|de|fi|tr)\s [NC]
RewriteRule ^ https://%2.domain.com/%1? [NE,R=301,L]
# internal rewrite rule to add lang parameter back
RewriteCond %{QUERY_STRING} !(^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^(es|de|fi|tr)\. [NC]
RewriteRule ^ %{REQUEST_URI}?lang=%1 [L,QSA]
Could you please try following .htaccess Rules at the top of your htaccess file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##use THE_REQUEST variable to match condition here, with NC flag.
RewriteCond %{THE_REQUEST} \s/([^?]*)\?&?lang=(es|de|fi|tr)\s [NC]
RewriteRule ^ https://%2.domain.com%1 [NE,R=301,L]

How do I ignore the end of a url parameter string in Htaccess?

I have a bunch of ugly links coming in to a site like this:
https://www.somedomain.com/mm5/mch.mvc?Session_ID=5f59c6e0&Screen=PRODFB&Product_Code=pcode1&Category_Code=category_a&Store_Code=store1&fb=1
I'm trying to use htaccess to recreate the url like this:
https://www.somedomain.com/mm5/mch.mvc?Screen=PROD&Product_Code=pcode1
I've come up with this but of course it isn't working. I think I just need to be able to ignore the rest of the url parameter after the product_code param but not sure how to do it.
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^Screen=PRODFB&Product_Code=([^/.]+)$ /mm5/mch.mvc?Screen=PROD&Product_Code=$1 [R=301,L,NE]
What am I missing? Thanks!
You cannot match query string in RewriteRule directive.
As long as query parameters are same as what you have in question, you may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(mm5/mch\.mvc)\?.*&Screen=PRODFB&(Product_Code=[^\s&]+) [NC]
RewriteRule ^ /%1?Screen=PROD&%2 [R=301,L,NE]
Or using %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} (?:^|&)Screen=PRODFB&(Product_Code=[^&]+) [NC]
RewriteRule ^mm5/mch\.mvc/?$ %{REQUEST_URI}?Screen=PROD&%1 [R=301,L,NE]
I was able to get it work like this:
RewriteCond %{QUERY_STRING} Screen=PRODFB&Product_Code=([^&]+)
RewriteRule ^(.*)$ /mm5/mch.mvc?Screen=PROD&Product_Code=%1& [R=301,L,NE]

htaccess rewrite - Hide subfolder from URL

We have a page file here:
ourdomain.com/subfolder1/subfolder2/
But we'd like people to be able to access it by going here:
ourdomain.com/subfolder2/
So we need both a redirect and a rewrite.
Any idea how this can be achieved using htaccess? I've tried a million things and nothing is properly rewriting/redirecting.
What I currently have...
RewriteCond %{REQUEST_URI} ^/subfolder1/subfolder2/?$ [NC,OR]
RewriteRule ^(.*) http://www.ourdomain.com/subfolder2/ [R=301,L]
You can use:
# Redirect from /subfolder1/subfolder2/...
RewriteCond %{THE_REQUEST} \s/+subfolder1/(subfolder2/[^\s?]*) [NC]
RewriteRule ^ %1 [R=301,L]
# Rewrite to /subfolder1/subfolder2/...
RewriteRule ^(subfolder2/.*) /subfolder1/$1 [NC,L]

Remove Query String from Rootdomain

I know with a simple redirect one (Sub-)Domain or Folder to another i can get rid of a string like this
RewriteEngine On
RewriteRule (.*) http://www.domain.tld/? [R=301,L]
I know how to get rid of it when it is a simple file too.
But when it comes to the Rootdomain itself (http://domain.tld/?Stringwhatsoever), i am at a loss here. My last try used a modified version of a redirect I used to redirect files and folders around and that worked pretty nicely and also removed the query, but it ended up in a redirection error.
RewriteRule ^ http://domain.tld/? [L,NC,R=301]
So i have no clue how to get rid of Query Strings at urls without breaking it.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /\?([^\s]+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Or
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+) [NC]
RewriteRule ^$ http://domain.com/? [NC,R,L]
Reference :
-https://wiki.apache.org/httpd/RewriteQueryString

Resources