URL redirect in htaccess file - php

I want to ask how to redirect in htaccess file from same url with query string parameters to same url without query string parameters. For example if my url is www.example.com/abc.php?cid=5&scid=10, I want to redirect it to www.example.com/abc.php
Please note that the url is same, just query string parameters have been removed in redirection. I have written htaccess file code for urls with single query string but with more than one parameters it is not redirecting.
Here is my code for single parameter which is working as expected:
RewriteCond %{QUERY_STRING} cid=7$
RewriteRule (.*) /contact-us.php? [R=301,L]
It redirects contact-us.php?cid=7 page to contact-us.php
But when I'm exteding the code for multiple parameters it doesn't redirect. Here is what I have written in this case:
RewriteCond %{QUERY_STRING} cid=9$
RewriteCond %{QUERY_STRING} scid=14$
RewriteRule (.*) /stakeholders.php? [R=301,L]
This one is supposed to redirect stakeholders.php?cid=9&scid=14 page to stakeholders.php page. Can someone tell me what is wrong in second code?
I have also googled it but almost all the results are about redirecting from one url to another url not to same url with query string parameters removed. Thanks.

You need to connect your query into one condition, like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} cid=9&scid=14 [NC]
RewriteRule ^(.*)$ http://www.example.com/stakeholders.php? [R=301,NC,L]
This will work for the specific query of cid=9&scid=14, if you wanted to do it for any query then you could use cid=(.+)&scid=(.+) instead.
The ? on the end of the rewritten URL stops the original query from appearing on the end of it and it uses a 301 redirection to perform the change.

Related

Add New Parameter to Existing URL using htaccess

I have a url like below
http://filespecification.phpnet.us/index3.php
But i want to add an additional parameter to this url like below.
http://filespecification.phpnet.us/index3.php?email=
For this i am using .htaccess to redirect with additional parameter but it is not redirecting still show original url.
RewriteRule /index3.php$ index3.php?email=$1 [R]
But it is not working. How can i redirect with additional parameter?
Try this simple one. Hope this will be helpful. I have tested it.
1. QUERY_STRING does not contain email.
2. if request uri is index3.php redirect to /index3.php?email=somevalue
Flags:
R for redirection.
L last redirection.
QSA query string append
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(?!email)
RewriteRule index3.php$ /index3.php?email=somevalue [L,R,QSA]
You can check this .htaccess code here http://htaccess.mwl.be/
Your rule isn't working due to the leading slash. You need to remove it.
RewriteCond %{THE_REQUEST} !\?email= [NC]
RewriteRule index3.php$ /index3.php?email=mail [L,R]

htaccess url rewrite url containing question mark

I'm having trouble redirecting some urls as below, there are many urls with different cat breeds. Can anyone help with this issue.
Example url to redirect :
http://www.exampledomain.co.uk/cats/database.nsf/catsforsale!openform?Breed=Persian
I want it directing to the url below. My php script should then do some more complex redirect to make the url tidy :
http://www.exampledomain.co.uk/display_pets.php?pettype=Cats&petbreed=Persian
I've tried the rewrite below but it doesnt work, it just doesnt redirect at all, i think it may have something to do with the ? :
RewriteRule ^/cats/database.nsf/catsforsale!openform?Breed=(.*)$ display_pets.php?pettype=Cats&petbreed=$1 [L]
RewriteRule normally does not look at query strings.
You need to use the QSA flag with Rewrite. It combines the query strings and appends it to the target url.
You can try the following:
RewriteRule ^(.*)$ display_pets.php?url=$1 [L]
This will pass the entire source url to the url parameter of the target url. It will also append the query strings of the source url to the target url.
Another option is to match the query string inside RewriteCond.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^/cats/database.nsf/catsforsale!openform$ display_pets.php?pettype=Cats&petbreed=%1 [L]
See following links:
RewriteRule Flags and
Manipulating the Query String

htaccess rewriteRule not displaying new url

My question is how to get the URL in the RewriteRule to display when I click the link (domain.com/?brandID=1&name=Beretta).
My tests make me believe it might have something to do with index.php, but maybe not. My script is triggered by the GET variable being set, and queries the DB and includes the page that displays the results (and the "ugly" url).
Here's the .htaccess code:
# Rewrite for ?brandID=1&name=Name
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]
I want to replace domain.com/?brandID=1&name=Beretta with domain.com/pistol-brand/1/beretta
The code above displays the "ugly" url, but when I change the url to what the RewriteRule states (domain.com/pistol-brand/1/beretta), the page works.
How do I get the "new" url to display when the link is clicked?
Thanks for any assistance.
You need one more to redirect the ugly url to sef format
##1)Redirect /?brandID=foo&name=bar to //pistol-brand/foo/bar##
RewriteCond %{THE_REQUEST} /\?brandID=([^&]+)&name=([^\s&]+) [NC]
RewriteRule ^ /pistol-brand/%1/%2? [L,R]
##2)Rewrite /pistol-brand/foo/bar to /?brandID=foo&name=bar##
RewriteRule ^pistol-brand/([0-9]+)/([0-9a-zA-Z_-]+) ?brandID=$1&name=$2 [NC,L]

How to orgnize 301 redirects only for pages with get parmeters?

Here is a quick SEO question. We decided to split our web site into two different sites. Google has already crawled 50K pages which we want to move to another domain name. My question is what would be the best way to deal with it as we want only certain URLs to be redirected not the whole website. Should I do mode rewire catch the get parameters and send them over to the new domain name? or should I do it with php headers?
olddomain.com becomes oldomain.com and newdomain.com
oldomain.com?name=jw&gsurname=black --> newdomain.com?name=jw&gsurname=black
oldomain.com with any other url structure should stay the same
You should be able to use the RewriteCond and RewriteRule directives together to match against query string values, like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^name=([^&]+)&gsurname=([^&]+)$
RewriteRule ^(.*)$ http://newdomain.com?name=%1&gsurname=%2 [R=301,L]
If you mean exactly that set of query parameters:
RewriteCond %{QUERY_STRING} ^name=([^&]+)&gsurname=(.*)$
RewriteRule ^/$ http://newdomain.dom [R=301, L]
The RewriteCond looks at the query string and checks that the first parameter is name and the second parameter is gsurname. The ([^&]+) collects all of the characters until it finds an ampersand (^& means not ampersand). The (.*)$ collects characters until the end of the query string ($).
If the RewriteCond is true, then the RewriteRule redirects to the new domain. The query string is automatically passed along as-is.

Rewrite url which redirects to page on webserver

I am trying to get a page with a query string to redirect to a nicer looking url then get that url and transfer it back to the original query string but without redirecting (i.e. without changing the url)
At the moment I am getting a redirect loop (for obvious reasons) but I was hoping for a way to stop this.
This is my code in my htaccess file
#rewrite search querystring
#/search/'apartment'/2_bedrooms/price_0-500000/town_W4/development_18,SW5/
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) /search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
/search/$1/$2_bedrooms/price_$3-$4/town_$5/development_$6 [R,L]
RewriteRule ^(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6? [R,L]
So what it is meant to do is:
user has been taken to:
http://www.domain.com/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
This page is the actual page on the server where the data is coming from, however I want the user to see.
http://www.domain.com/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
Is it possible to do this without a redirect loop.
Thanks for your help
EDIT I'm thinking it could be done with the rewrite flags but I'm not sure, I'm quite new to the Rewrite Engine
Edited:
Here is a complete (and working) solution for you:
RewriteEngine On
# User gets here:
# http://localhost/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
# He is explicit redirected to here:
# http://localhost/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
# Internally, apache calls this:
# http://localhost/search.php?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC,PT]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
RewriteRule ^search/(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6/? [R,L]
It assumes you put .htaccess in server root and that there is a file search.php in root too.
Original:
I think you can use PT and QSA Rewrite Rule flags (http://httpd.apache.org/docs/current/rewrite/flags.html) in your first rule
Use PT for server-side redirection (it will not change the URL for the user/browser, but will for your server-side scripts)
Use QSA if you wanna carry the query while doing this redirection
You can redirect all requests that don't target an existing file to a specific php-script, for example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT,QSA]

Categories