Forum Moderators: coopster
domain.com/?pagename to domain.com/pagename
so i just want to remove the ? character
what is the right Rule in htaccess for it guys?
i try below but its not working, its say error 404
RewriteRule [domain.com...] [domain.com...]
this is working to redirect a page, but i need to redirect every pages
Redirect 301 /?pagename [url]http://www.example.com/pagename[/url]
You don't "change" query string URL's to "friendly urls." You direct the friendly URL's to the scripts.
On your site you link to
domain.com/pagename
the mod_rewrite rule redirects to say, a script.
RewriteRule ^(pagename)$ /script.php?$1
Anything in () gets stored in $1. So this would effectively pass pagename to your script, without a value. so if (isset($_GET['pagename'])) **should** return true. it just won't have a value.
The beauty of it is, both of these URL's will still work, and you don't really need to mod your scripts much, just add a few lines to capture the incoming variable 'pagename'.
domain.com/pagename
domain.com/script.php?pagename
JDMorgan's excellent mod_rewrite primer [webmasterworld.com]
Another from DaveAtIFG [webmasterworld.com]
You might need to "play" around with mod_rewrite to get a feel for what's going on.
1. Create a directory, name it test. Store all of the below in this directory.
2. Create this file, name it test.php:
<?php
header("content-type:text/html");
if (isset($_GET['pagename'])) { echo "Pagename was set"; }
else { echo "pagename was not set"; }
?>
3. Create this file, name it index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>mod_rewrite test</title>
</head>
<body>
<p><a href="pagename">Redirect to script</a></p>
</body>
</html>
4. Create this file, name it .htaccess:
RewriteEngine On
RewriteRule ^(pagename)$ /test/test.php?$1
Upload the entire directory and all files in it to your server, use ASCII mode if you are using FTP.
Request the directory, or, if this is forbidden (which means you have a problem with your server config,) request the index.html file:
http://www.example.com/test
or
http://www.example.com/test/index.html
(Substituting YOUR DOMAIN for example.com, of course.)
Click the link. What you should see is
pagename was set
But here's cool part #1. Look in the address bar, you should still see
http://www.example.com/test/pagename
Even though this is being processed by test.php. What is happening is you request /test/pagename, your .htaccess rule is rewriting that request to test.php, not redirecting it (apologies to rewrite experts for mangling the explanation . . . )
Now here's cool part #2 (not so cool for SEO, see below.) Unless you implement something to do otherwise, the script will still work with the query string. Type the following into the address bar (substitute example.com for your domain:)
http://www.example.com/test/test.php?pagename
You will get the same result, "pagename was set." What this means is old links can still access your friendly URL, but you now have to take measures to avoid duplicate content problems in search engines - two URL's accessing the exact same content - a topic for a different thread.
Examine the above example, check out the links I sent you, get a feel for how mod_rewrite works. You can ask advanced mod_rewrite questions in the Apache forum.
Obvious final test, call test.php directly or with some other parameter,
http://www.example.com/test/test.php
http://www.example.com/test/test.php?something-else
and you get "pagename was not set."
The next step you'll take after getting this is to set up your mod rewrite to pass any value of "pagename" as a variable in test.php. But get around this part first.