Page 1 of 1
Configuring my RewriteRule
Posted: Tue Nov 22, 2011 3:00 am
by social_experiment
I've started to look at using clean url's which means i have to know about RewriteRule. I have the following in my .htaccess
Code: Select all
RewriteEngine on
RewriteRule ^/?test\.html$ test.php [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)$ display.php?country=$1&state=$2&city=$3 [L]
According to the tutorial i am using this will render the following
http://www.example.com/USA/California/San_Diego yet on my host it doesn't; it simply displays the query string filled url. mod_rewrite is enabled and the first rewrite rule also works so i'm stumped about why this isn't working.
Am i correct in saying that if i access a link that has display.php?country=USA&country=California&city=San_Diego as an href value; i will be redirected to
http://www.example.com/USA/California/San_Diego ?
Re: Configuring my RewriteRule
Posted: Tue Nov 22, 2011 3:43 am
by maxx99
Nope its the other way around if you access
http://www.example.com/USA/California/San_Diego
Proper config will get you:
$_GET['country] = 'USA'; $_GET['state'] = 'California' etc.
There is no redirection
Re: Configuring my RewriteRule
Posted: Tue Nov 22, 2011 5:56 am
by social_experiment
http://www.example.com/USA/California/San_Diego
So my url in the href attribute should be this (above) if i want to access the page?
Re: Configuring my RewriteRule
Posted: Tue Nov 22, 2011 6:01 am
by maxx99
Yes
Browser's adress bar will show
http://www.example.com/USA/California/San_Diego
But youll be in fact on:
http://www.example.com/display.php?coun ... =San_Diego
Assuming that your rewrite rules are ok

Re: Configuring my RewriteRule
Posted: Tue Nov 22, 2011 6:05 am
by social_experiment
Ah thanks; i think that is my problem with understanding how mod_rewrite and rewrite rules work; i thought they redirected instead of rewriting. I'll try accessing the url as suggested and see what happens
Re: Configuring my RewriteRule
Posted: Tue Nov 22, 2011 7:52 am
by social_experiment
Just tested my script. I tested the url and when i print_r $_GET i get the following
Code: Select all
Array
(
[USA/California/San_Diego] =>
)
If i echo $_GET['state'], nothing is displayed

Re: Configuring my RewriteRule
Posted: Tue Nov 22, 2011 11:21 am
by pickle
That's an error in your regex - though at first glance I can't see what the problem is. Try taking out the ? at the beginning. It might be best to find an online regex checker so you can get the regex nailed down easily.
Here's mine:
Code: Select all
<?PHP
# Retrieve parameters from $_POST if present
if(isset($_POST['submit']))
{
$safe_pattern = (get_magic_quotes_gpc()) ? stripslashes($_POST['pattern']) : $_POST['pattern'];
$safe_target = (get_magic_quotes_gpc()) ? stripslashes($_POST['target']) : $_POST['target'];
}
# Evaluate if necessary
if(isset($_POST['submit']))
{
if(isset($_POST['all']))
preg_match_all($safe_pattern,$safe_target,$matches);
else
preg_match($safe_pattern,$safe_target,$matches);
$matches = (count($matches)) ? print_r($matches,TRUE) : FALSE;
}
?>
<html>
<head>
<title>
PERL Regular expression verifier
</title>
<style>
body{font-family:Verdana;}
</style>
</head>
<body>
<form name = "regex_form" method = "POST" action = "">
<div>
<p>
(PERL) Pattern:
<textarea name = "pattern" rows = "5" style = "width:100%;"><?= $safe_pattern ?></textarea>
</p>
<p>
Target:
<textarea name = 'target' rows = "10" style = "width:100%;"><?= $safe_target ?></textarea>
</p>
<label for = "all">
<input type = "checkbox" id = "all" name = "all"<?php if(isset($_POST['all'])): ?>checked="checked"<?php endif; ?>>Match all
</label>
<br />
<input type = "submit" name = "submit" value = "Check">
</div>
</form>
<?php if($matches): ?>
<pre><?= $matches ?></pre>
<?php elseif($matches === FALSE): ?>
Does not match
<?php endif; ?>
</body>
</html>
Re: Configuring my RewriteRule
Posted: Mon Jan 09, 2012 8:00 am
by simonmlewis
How did you ensure your images loaded correctly from index.php?
I am trying this clean url method, and while it is loading the include files correct, it is not loading the images, as it thinks they are in:
test.local/pages/selector/images/test.jpg
Rather than: test.local/images/test.jpg.
If I run it with the full URL it is fine, but when in Firefox I right click and View Image, it thinks it's in that long URL.
How do you resolve that?