Passing a Query String via .HTACCESS - string

I have a dynamic query string that I need to pass via an .htaccess redirect. For example:
I need to redirect this URL: http://mysite.com/page1?action=signup&var2=dynamicVar
To this: http://mysite.com?action=signup&var2=dynamicVar
I know this is pretty simple, but I'm really not sure what type of rule/syntax would work for this.
Any help is greatly appreciated!

If you already have .htaccess then simply add this line:
RewriteRule ^page1/?$ page2 [L,R,QSA,NC]
Update: Based on your comments:
RewriteCond %{QUERY_STRING} (^|&)action=signup(&|$) [NC]
RewriteRule ^page1/?$ / [L,R,QSA,NC]

RewriteRule ^page1(.*)$ /page2$1 [L,R=301]

Input
http://mysite.com/page1?action=signup&var2=dynamicVar
this is the rewrite rule
RewriteRule ^page1?(.*)$ /?$1 [L,R=301]
redirect to
http://mysite.com/?action=signup&var2=dynamicVar
this will redirect all request o page1 with get parameters to http://mysite.com

Use %{REQUEST_URI}
fi.:
RewriteCond %{HTTP_HOST} ^website\.(.+)$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
rewrites anything that starts with "website" to "www.website..." including the querystring (by use of %{REQUEST_URI})

Related

.htaccess help for redirect

Need some help trying to redirect a domain:
https://www.example.com/blog/page/10/?page_id=%2Ffeed%2Fatom%2F to /blog/
I think the best way is to just redirect anything after /page
The code below is what I'm thinking but I know is not correct:
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/[0-255]* [NC]
RewriteRule ^ /blog/? [NC,R=301,L]
Any advice?
Need some help trying to redirect a domain:
If you are trying to redirect the domain use something like
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteRule ^(.*)$ "http://www\.exampe2\.com\/blog/" [R=301,L]
If you want to append the request to the URL use $1
If you're rewriting the path to /blog/ and want to keep the query string use the [QSA] flag ie.
RewriteRule /blog/(.*) /blog/ [R=301,QSA,L]
this will make sure you keep your query string.

change the url using with htaccess

i am trying to change the url using htaccess rewrite
i want to change this url
page/details.php?name=abcdef&id=18
to
page/abcdef
Here my sample code for this
RewriteEngine On
RewriteRule ^company/([A-Za-z0-9-]+)/$1 company/details.php?name=$1&id=$2 [R=301,L]
this is not working, and also i was tried many code but not working,please help me to find htaccess code for this url
Thanks advance
This should do what you're after:
RewriteCond %{QUERY_STRING} ^.*name=([a-zA-Z0-9]*).*$
RewriteRule ^page/(.*)$ page/%1? [R=301,L]
Try this one:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^name=(.*)&id=(.*)$
RewriteRule ^page/(.*)$ /page/%1? [R=301,L]
This will check for these 2 parameters in the query string.

.htaccess 301 redirect to the same page with no query string

I want to create a condition that if page has parameter in URL like ?print=1 - redirect this page to itself without any querystring.
Example:
I want this page:
http://sos-med.com/en/specific_page.html?print=1&ok=1
tp redirect (301) to the same URL with no Query string:
http://sos-med.com/en/specific_page.html
My code is:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?
I have no test server, so can you tell me if my code is ok?
The code above is for every page on website. And before implementing that rule I would like to try the redirect for one specific page (see my example).
How to modify the code to work with "specific_page.html" only?
I want only .htaccess solution, not PHP code.
You're close, your %{QUERY_STRING} regex isn't right, and you're missing the 301 redirect flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]
Try that.
Thanks, and If I want to redirect single specific page: sos-med.com/en/aaa.html?print=1&ok=1 to sos-med.com/en/aaa.html ? –
Then you'd change what the rule matches against:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
Try this one instead :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
RewriteRule ^(.*)$ /$1? [L,R=301]

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

htaccess redirect query string and discard

Having trouble with redirecting a dynamic URL. This is what I want to accomplish:
Redirect 301 /content/index.php?id=423 http://www.domain.com/new-page/
I tried this
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=423$
RewriteRule ^content/index\.php$ http://www.domain.com/new-page [L,R=301]
but had no luck. Thank you!
ps/
I lost some para
work with this code
RewriteRule ^([^.]+)/([A-Za-z]*)-([^.]+)-([0-9]+).html$ $1/$4-$2-$3.html [L,R=301]
You need to specify the query in the replacement URL. Otherwise the original query is taken:
RewriteCond %{QUERY_STRING} ^id=423$
RewriteRule ^content/index\.php$ http://example.com/new-page? [L,R=301]
And if you want to preserve other URL arguments, try this:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=423&*([^&].*)?$
RewriteRule ^content/index\.php$ http://example.com/new-page?%1%3 [L,R=301]

Resources