Forum Moderators: coopster

Message Too Old, No Replies

(Easy enough) What is the right RewriteRule inhtaccess for this.

         

basketmen

12:09 am on Sep 11, 2009 (gmt 0)

10+ Year Member



it is very simple actually, i just want redirect all pages from

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]

rocknbil

3:17 am on Sep 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've made a fundamental mistake that I think everyone makes with mod_rewrite: you are thinking it though backwards.

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]

basketmen

11:32 pm on Sep 11, 2009 (gmt 0)

10+ Year Member



hmm i am sorry i dont understand what your talking about

i had try this but still not working

RewriteRule ^$ /?$1

is there other rule bro?

rocknbil

2:02 am on Sep 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No that won't work. I'm sure J.D. can tell you exactly what it will do, but . . . it's not what you're after. :-)

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.