Apache RewriteRule on URL with query string - php

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]

Related

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

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

How can I rewrite an url and pass the full request url as parameter?

I want to rewrite an URL like http://www.domain.tld/test/page.html?id=1&required=1&foo=bar
to http://www.domain.tld/wrapper.php?url=[FULL REQUEST URL]
in wrapper.php I want $_GET['url'] to be http://www.domain.tld/test/page.html?id=1&required=1&foo=bar
What I've tried so far:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*required=.*)$ [NC]
RewriteRule ^ /wrapper.php?url=http://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} [L]
But unfortunately $_GET['url'] is now just http://www.domain.tld/test/page.html?id=1 with the other parameters missing.
How should the correct apache synthax for this look like?
Try adding QSA to the Rewrite rule
[L,QSA]
You can use a rule like this in your root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)required=[^&]+ [NC]
RewriteRule !^wrapper\.php /wrapper.php/http://%{HTTP_HOST}%{REQUEST_URI}#%{QUERY_STRING}? [L,NC]
RewriteCond %{REQUEST_URI} ^(/wrapper\.php)/([^#]*)#(.+)$ [NC]
RewriteRule ^ %1?url=%2\?%3 [L,B]

replacing Login URL to short URL with .htaccess

I have an URL as :
http://mydomain.com/site/?cmd=home
I want to change the above address to http://mydomain.com/home
i am using .htaccess file like this:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /site?cmd=$1 [L]
I am not sure where is the problem?
Thanks in advance
The Rule works the other way around: you want to get from /site/?cmd=home to /home, but with your RewriteRule you are redirecting to /site?cmd= (Probably there is a mistake somewhere?)
If you want to redirect from /site/?cmd=home to /home, like the question states, use the following:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/site
RewriteCond %{QUERY_STRING} ^cmd=(.*)$
RewriteRule ^(.*)$ /%1/? [R=302,L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /site\?cmd=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^([^/.]+)/?$ /site?cmd=$1 [L]

Redirect page with GET variable htaccess

Firstly,Thanks for reading this. How to rewrite the url , in htaccess
For example
wwww.domain.com/home?name=MuhdNazmi <---OLD URL
wwww.domain.com/home/MuhdNazmi <---NEW URL
name is a get variables,
I dont need put .php , since it code for not showing > .php
Try:
RewriteCond %{THE_REQUEST} \ /home\?name=([^&\ ]+)
RewriteRule ^ /home/%1? [L,R=301]
RewriteRule ^home/([^/]+)$ /home?name=$1 [L,QSA]
RewriteEngine On
RewriteRule ^home/([^/]*).php$ /home?name=$1 [L]
If it is a one on redirect then you can follow the below example for you redirect
RewriteEngine on
RewriteCond %{QUERY_STRING} name=MuhdNazmi$ [NC]
RewriteRule ^home/$ /home/MuhdNazmi? [L]
If you want to define it as a generic rule for all the query string parameter modify the above code as
RewriteEngine on
RewriteCond %{QUERY_STRING} name=(.*)$ [NC]
RewriteRule ^home/$ /home/%1? [L]

Categories