Reputation: 11
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.
Upvotes: 1
Views: 235
Reputation: 785866
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]
Upvotes: 0
Reputation: 9121
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.)
Upvotes: 2
Reputation: 17010
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.
Upvotes: 0