1

is it possible to make it so that url rewrites are forced?

Here is an example of what works, http://localhost/home/ goes to http://localhost/index.php?page=home but the url stays the same to the user.

What I can't get to work is forcing http://localhost/index.php?page=home to display as http://localhost/home/.

I don't know if I'm using the right terminology, but I want it so that if you type the non rewritten url, I want the rewritten url to appear in the user's browser.

Here is what I have working so far:

RewriteEngine on
Options +FollowSymLinks

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)/?$ index.php?page=$1&a=$2&b=$3 [L,NC,QSA]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^([a-zA-Z0-9_]*)/([a-zA-Z0-9_]*)$ index.php?page=$1&a=$2 [L,NC,QSA]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^([a-zA-Z0-9_]*)$ index.php?page=$1 [L,NC,QSA]
TheAutumnAurora
  • 123
  • 1
  • 7
  • You cannot change the url entered by the user without reloading the page. I do not think a rewrite rule will help you in this scenario. (See http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) – tawman Mar 16 '11 at 13:45
  • I think you take it the wrong way: if you think about it, you just don't want users to access directly to `index.php`, right? Just do a **`404`** and in the **`404`** display some links that **really** work. Nice 404 have messages like "Did you mean `http://localhost/home/`"? ... – Olivier Pons Nov 29 '11 at 09:40

2 Answers2

1

That's what the [R] flag is for. If you have a correctly-working rewrite, you'll want to add [R] to your other flags at the end of your RewriteRule.

Andrew
  • 14,325
  • 4
  • 43
  • 64
  • Won't the [R] make it "http://localhost/index.php?page=home" if i enter "http://localhost/home/"? If it does, this is the opposite effect. I want it to become "http://localhost/home/" when i enter "http://localhost/index.php?page=home" to the user's browser. I want this so that my links work whether mod_rewrite is enabled or not. _On a side note, I don't know why there are semicolons after my links in this comment, ignore them..._ – TheAutumnAurora Mar 15 '11 at 16:27
0

Try adding some code to your index.php.

Look at the $_SERVER['SCRIPT_URL'] variable for your answer. When you enter "localhost/index.php?page=home" in your browser, SCRIPT_URL should equal '/index.php'. If you enter "localhost/home", SCRIPT_URL should equal '/home'. You can do a check for this in your PHP script and redirect appropriately from there.

WNRosenberg
  • 1,862
  • 5
  • 22
  • 31