.htaccess and .php? redirection - php

I'm editing a .htaccess file for redirection from www.oldwebsite.com to www.newwebsite.com + specific redirection for pages.
the following code is working good :
RedirectPermanent /page.html http://www.newwebsite.com/newpage.html
Where I got problem is when I try to redirect pages ending like this with redirectpermanent :
oldpage.php?id=1
At this point I get a 404 error back.
I tried another solution that is this code
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html [R=301,L]
This is working excepted the browser makes me go to the following link :
http://www.newwebsite.com/newpage2.html?id=1
Can someone help me with this issue. I would like to use Redirect permanent (doesnt work, same thing with Redirect seeother). I think solution is easy but I don't get a detail I think.
Thanks !!!

just add ? at the end of rewriteRule to override query string
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html? [R=301,L]

You can use PHP $_SERVER['PHP_SELF'] method for this..
so,
$page = $_SERVER['PHP_SELF'];
header("Location: http://www.newwebsite.com$page");
It will redirect http://www.oldwebsite.com/newpage.html to http://www.newwebsite.com/newpage.html
or instead of $_SERVER['PHP_SELF'] you can try $_SERVER['REQUEST_URL']

If you are trying to drop the query string, you can use the QSD flag.
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^oldpage\.php$ http://www.newwebsite.com/newpage2.html? [R=301,L, QSD]

Related

issue with .htaccess using 301 Redirection with params

i want to stop parameters from getting appeneded to url
issue with .htaccess i have website say example.com
1: i have to redirect
example.com/deatils.php?366%2520WORDS%2520IN%2520MUMBAI example.com/bookshelf/366_words_in_mumbai
i wrote below code
RewriteRule ^deatils.php?366%2520WORDS%2520IN%2520MUMBAI example.com/bookshelf/366_words_in_mumbai [R=301,L]
but opening
example.com/deatils.php?366%2520WORDS%2520IN%2520MUMBAI
redirect to example.com/bookshelf/366_words_in_mumbai?366%252520WORDS%252520IN%252520MUMBAI
i do not want strings after "?" to be appened
Use this additional rule for the redirection in your htaccess and it will work for you.
RewriteCond %{QUERY_STRING} (^|&)366%2520WORDS%2520IN%2520MUMBAI($|&)
RewriteRule ^deatils\.php$ /bookshelf/366_words_in_mumbai? [L,R=301]

Redirect 301 htacces with question mark 404 file not found

I want to redirect
/fr/verhuur_details.asp?nav=3&id=106
TO
/fr/location/knikarm
Im using this rule in my .htaccess:
Redirect 301 /fr/verhuur_details.asp?nav=3&id=106 /fr/location/knikarm
But i always receive 404 not found
But code below works perfect
Redirect 301 /fr/verhuur_details.asp /fr/location/knikarm
everything is working fine. So my guess is that the ? is causing this problem. Any kind of help will be well appreciated! Thanks in advance
Not so much used to Redirect, but with RewriteRule, you could go like this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^nav=3&id=106$
RewriteRule ^fr/verhuur_details.asp$ /fr/location/knikarm? [R=301,L]
This should work.
Note : if you want keep query string after redirect, you'll need to delete ? at the end :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^nav=3&id=106$
RewriteRule ^fr/verhuur_details.asp$ /fr/location/knikarm [R=301,L]
Hope it helps.

modify URL using .htaccess

I want to change url for example http://www.mysite.com/cgshop/admin/index.php?route=common/home will become http://www.mysite.com/cgshop/cp/index.php?route=common/home.
Please help me to figure out this. Thanks in advance.
This should get you what you want.
It tests to see if the URI begins with /cgshop/admin/ and then captures the rest so it can redirect with a 301 to the new url. The QSA means it will also carry over the query string (everything after the ?).
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgshop/admin/.*$
RewriteRule ^cgshop/admin/(.*)$ /cgshop/cp/$1 [R=301,L,QSA]
RewriteEngine On
RewriteRule ^cgshop/cp/.*$ cgshop/admin/$1
Try this.......

How can I write a .htaccess redirect to find all numbers?

I'm trying to figure out a regular expression that will find some numbers in a URL and use them in the URL I redirect to.
Redirect 301 /page.php?id=95485666 http://test.com/profile/info/id/95485666
i was thinking maybe
Redirect 301 /page.php?id=([0-9]+) http://test.com/profile/info/id/$1
but it doesn't seem to work
Also, if I do a 301 redirect, how long do i have to keep the code in the .htaccess file? when is Google gonna figure out that the new link is the good one?
You can't match against the query string in the Redirect directive (nor in a RedirectMatch/RewriteRule either). You need to use mod_rewrite's %{QUERY_STRING} var:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)id=([0-9]+)($|&)
RewriteRule ^/?page\.php$ http://test.com/profile/info/id/%2? [L,R=301]
Well I guess the syntax is incorrect. Try this:
RewriteRule ^page.php?id=([0-9]+) http://test.com/profile/info/id/$1 [R=301,L]

mod rewrite not working in .htaccess

I am trying to write rewriterule for php site.
My URL is like http://example.com/attorneys?pid=69
I write in .htacess as below:
RewriteEngine On
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L]
Both the link example.com/attorneys?pid=69 and example.com/attorneys/69 works.
How can I make the browser know that if it get the first link it have to show the second one in browser.
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L,R=302]
So you want to redirect http://xyz.com/attorneys?pid=69 to http://xyz.com/attorneys/69? Another rule after(!) the first rule should do the trick:
RewriteEngine On
RewriteRule ^attorneys/([0-9]+)/?$ attorneys&pid=$1 [NC,L]
RewriteRule ^attorneys&pid=([0-9]+)$ attorneys/$1 [NC,L,R=301]
Because the first rule is marked with the L flag, the second won't be executed if the first matches. (See the documentation of mod_rewrite flags here.)
First, I need to say you there is no need to do that. Anyway, I was forced to do it in the past for a SEO-maniac client. I tell you, that's not an elegant solution!
On top of the attorneys PHP page (I don't know if it's a directory with index.php or not, but you know it) add this code:
// Get request script
$request = preg_split('/[\\/?&]/', $_SERVER['REQUEST_URI']);
$request_script = $request[1];
// Check old URL
if ($request_script == 'attorneys') {
// Redirect
header('Location: /attorneys/' . $_GET['id'];
exit();
}
Maybe it's not exactly like this for your case, but I hope you get the mechanism.
This appears to be a simple ask. Try this code in your .htaccess file under DOCUMENT_ROOT (and comment out your existing code):
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteOptions MaxRedirects=5
RewriteRule ^(attorneys)/([^/]+)/?$ $1?pid=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^pid=(.*)$ [NC]
RewriteRule ^(attorneys)/?$ /$1/%1? [NC,L,R=301]

Categories