.htaccess rewrite with HTTP_HOST and QUERY_STRING - php

I would like to set up a redirect to a new host with query string. I am struggling to get the query added to the end of the target url. Here is my example old url
http://sub.oldexample.com/folder1/page1?value=ABC123
I would like it to go to
http://newexample.com/folder3/page6?value=ABC123
I do not have a problem when a query is not involved, for example the following code works perfectly but is unsuitable
RewriteCond %{HTTP_HOST} ^sub.oldexample.com$
RewriteRule ^folder1/([A-Za-z][A-Za-z][A-Za-z][0-9][0-9][0-9])$ http://newexample.com/folder3/$1 [L,R=301,NC]
I have tried to include a query condition but it fails
RewriteCond %{HTTP_HOST} ^sub.oldexample.com$ [NC]
RewriteCond %{QUERY_STRING} ^(value=ABC123)
RewriteRule ^$ http://newexample.com/folder3/page6?%1 [L,R=301,NC]
I find the following link a perfect environment for testing a .htaccess rule as it gives the output url that is generated
http://htaccess.madewithlove.be/

Following rule should work for you:
RewriteCond %{HTTP_HOST} ^sub\.oldexample\.com$ [NC]
RewriteCond %{QUERY_STRING} (^|&)value=ABC123(&|$) [NC]
RewriteRule ^folder1/page1/?$ http://newexample.com/folder3/page6 [L,R=301,NC]
You don't need to copy QUERY_STRING as it will be automatically carried over to new URL.

Try this:
RewriteCond %{HTTP_HOST} ^sub.oldexample.com$ [NC]
RewriteCond %{QUERY_STRING} (.+)=(.+)
RewriteRule ^/?folder1/page1$ http://newexample.com/folder3/page6 [QSA,R=301]
Just try to use the [QSA] flag.

Related

.htaccess rewrite all, except one page

I am trying to solve a problem with my .htaccess file.
I would like to use 301 redirect from my "old" doman to the "new" one, except some certain pages. Like "PageA" and "PageB".
The code thats working and iam using for redirect all is this:
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301,NC]
"PageA", and "PageB" has to be redirected to the "new" domain too, but to a certain address. Like:
old.com/pageA ---> new.com/something/pageA
old.com/pageB ---> new.com/something2/pageB
What code should i add to .htaccess, to add some exceptions with certain addresses ?
Try below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(pageA|pageB)$
RewriteRule ^ http://www.new.com/test/%1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com [NC]
RewriteRule ^ http://www.new.com/$1 [L,R=301,NC]

Appending query string through redirect htaccess

I am trying to create a rewrite cond in htaccess
I am trying to redirect
http://www.example.com/business/search-results.php?keywords=Animal+Business+Cards&go=Search
to
http://www.example.com/business/search results.php?keywords=Animal+Business&go=Search
or if possible remove the word "+cards" from any search queries...
what I have tried so far
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Animal+Business+Cards$
RewriteRule ^(.*)$ http://www.example.com/business/search-results.php?keywords=Animal+Business [R=301,L]
also tried
RewriteCond %{QUERY_STRING} "&go=Search" [NC]
RewriteRule (.*) /$1? [R=301,L]
and this
Redirect /business/search-results.php?keywords=Animal+Business+cards&go=Search http://www.example.com/business/search-results.php?keywords=Animal+Business&go=Search
None of these working...
is it possible?
Can any one help?
Thank You in advance
Try the following code :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^keywords=([^\+]+)\+([^\+]+)\+([^&]+)&go=([^&]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R]
Another solution
RewriteEngine On
RewriteCond %{THE_REQUEST} \?keywords=([^\+]+)\+([^\+]+)\+([^&]+)&go=([^&\s]+) [NC]
RewriteRule ^ %{REQUEST_URI}?keywords=%1+%2&go=%4 [NC,L,R]
Note : Redirect directive does not support querystrings in its old path perameter, that is why your last attempt failed. You can use %{QUERY_STRING} or %{THE_REQUEST} variable to match against urls with query strings.

htaccess rewrite URL complex issue

http://my-domain.com/ppc-landing-page.php?Campaign=Boynton-Beach&AdGroup=Security%20System&Keyword=The%20Best%20Home%20Security%20System%20Company
I want this to be rewritten as
http://my-domain.com/Boynton-Beach/Boynton-Beach.php?Campaign=Boynton-Beach&AdGroup=Security%20System&Keyword=The%20Best%20Home%20Security%20System%20Company
and display the content inside ppc-landing-page.php
The idea is to get the value of Campaign query parameter and rewrite the url in the browser to
http://my-domain.com/{$_GET['Campaign']}/{$_GET['Campaign']}.php?Campaign=Boynton-Beach&AdGroup=Security%20System&Keyword=The%20Best%20Home%20Security%20System%20Company
the full query string should be there
and then it should replace the $_GET values in the ppc-landing-page.php with the values in the query string and display the content.
Try the following RewriteRule in the .htaccess of your site's document root
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} campaign=([^&]+) [NC]
RewriteRule ppc-landing-page.php %1/%1.php [R=302,L]
RewriteCond %{REQUEST_URI} !^/ppc-landing-page.php
RewriteCond %{QUERY_STRING} campaign=([^&]+) [NC]
RewriteRule ^(.*)$ ppc-landing-page.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/ppc-landing-page.php
RewriteCond %{QUERY_STRING} campaign=([^&]+) [NC]
RewriteRule ppc-landing-page.php %1/%1.php [R=302,L]
Now I have put something like this...First it worked now it's not ???

.htaccess Dynamic sub domains

I want to have dynamic subdomains, such as:
(.*).mydomain.com
that I can retreive with $_GET['subdomain']. I have added an wildcard DNS record (* A xx.xx.xx.xx), and now for every subdomain I go to (e.g. test.mydomain.com) all I get is:
Apache is functioning normally
I have tried this code in .htaccess, but nothing seems to help:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/?subdomain=%1 [L]
Thanks in advance!
Are you looking for something more like this?
RewriteCond %{QUERY_STRING} !subdomain=
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA]

Url rewrite from .htaccess with Get variables

I am working in a project in PHP , I am not using MVC but keeping a single index.php files which changes it's content based on $_GET['module']
My current url format is
http://phpquiz.com/school/index.php?module=user_groups&mid=13&cmid=32
Now i want to change it to
http://phpquiz.com/school/index/user_groups/13/32
I used following script and was able to rewrite url but my site stopped working as it lost get variables.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/school/index.php [NC]
RewriteCond %{QUERY_STRING} ^module=(.*)&mid=(.*)&cmid=(.*)
RewriteRule (.*) http://phpquiz.com/school/%1/%2/%3.asp? [R=301,L]
How can i do this by keeping get vatiables.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/school/index.php [NC]
RewriteCond %{QUERY_STRING} ^module=([^&]+)&mid=([^&]+)&cmid=([^&]+)
RewriteRule ^ http://local.htaccess/school/index.php/%1/%2/%3.asp? [R=301,L]
You seem to be doing reverse. Have this rule:
RewriteEngine On
RewriteRule ^school/index\.php/([^/]+)/([^/]+)/([^/]+)\.asp$ /school/index.php?module=$1&mid=$2&cmid=$3 [NC,QSA,L]

Categories