Configuring my RewriteRule

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Configuring my RewriteRule

Post 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 ?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Configuring my RewriteRule

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Configuring my RewriteRule

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: Configuring my RewriteRule

Post 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 :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Configuring my RewriteRule

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Configuring my RewriteRule

Post 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 :?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Configuring my RewriteRule

Post 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>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Configuring my RewriteRule

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply