htaccess rewrite URL complex issue - php

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 ???

Related

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.

using ,htaccess redirect example.com?id=12 to example.com/index.php?id=12

I want to redirect "example.com?pid=12" to "example.com/index.php?pid=12" using .htaccess
I have written following in my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} pid [NC]
RewriteRule ^ example.com/index.php\?$1 [R=301,L]
But it is not appending the query string.
You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^pid=\d+ [NC]
RewriteRule ^/?$ /index.php [R=301,L]
Query string is automatically carried over to the target.
You need to add the Query String Append flag [QSA] in the RewriteRule because you are modifying the query string with a "?"
More info: https://wiki.apache.org/httpd/RewriteQueryString

Apache RewriteRule on URL with query string

I need to rewrite a URL with a query string using the .htaccess file.
The actual value in the query string is not important.
I need to send /event/event.php?t=2 to /event/challenge
How can I do this?
I've tried the following but no joy:
RewriteRule ^event/event.php?t=2$ /event/challenge [L,QSA,NC]
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+event/event\.php?t=2[&\s] [NC]
RewriteRule ^ /event/challenge? [R=302,L]
RewriteRule ^event/challenge/?$ /event/event.php?t=2 [L,QSA,NC]
You must add a RewriteCond before rule:
RewriteCond %{QUERY_STRING} ^t=2
RewriteRule ^event/event.php$ /event/challenge [L,QSA,NC]

Htaccess redirect some URLS to one domain and all other urls to another one

I need to redirect some URLS to one domain and all other urls to another one. For example the redirect code which should redirect mydomain.com/?p=221 and mydomain.com/?p=222 to Google and all other to another domain.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(css|png|gif|jpe?g|js)$ [NC]
RewriteCond %{REQUEST_URI} !(?p=221|?p=222)& [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} (?p=221|?p=222)& [NC]
RewriteRule ^(.*)$ http://google.com [L,R=301]
But the code doesn't work. Why is that?
The query is not in the variable REQUEST_URI. You have to use the QUERY_STRING variable. Replace both conditions with these ones, respectively:
RewriteCond %{QUERY_STRING} !p=(221|222) [NC]
# And
RewriteCond %{QUERY_STRING} p=(221|222) [NC]

How to write a rewrite rule to hide query string?

I am having issues rewriting URLs to hide the query string. I am trying to rewrite:
http://seynol.tk/comments?post_referrel_id=16
to:
http://seynol.tk/comments only
This is what I have tried so far:
RewriteBase /
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule . %{REQUEST_URI}.php [L]
All you have to do is check if the query string is set and do a redirect with an empty query string.
RewriteCond %{QUERY_STRING} post_referrel_id
RewriteRule (.*) $1? [R=301,L]
If you want to put the referral id in the URL and silently convert it into a query string, you would use this instead:
RewriteRule ^/comments/([0-9]+)$ /comments?post_referrel_id=$1
This assumes that the post id is numeric. The URL would look like this: http://sevnol.tk/comments/16

Categories