...

View Full Version : Apache URL Rewrite Expression



GSimpson
06-21-2010, 09:11 AM
Hey guys. Need a little help, not going quite how I imagine it to.

I've got an index file that opens database content based on the get variable i give it, so I'm using Apaches URL rewriting to change a directory in to that get variable, and stripping out the nasty stuff with php before it goes into the database.

So my php for the index file is all done and working well, just need a quick tip with the rewrite code.

At the moment it accepts both:
http://www.example.com/home
AND
http://www.example.com/home/
(forward slash difference)

The only problem is, when I use the forward slash, it must be accepting the index.php file I direct it to as one directory further and doesn't load images and stylesheets correctly.

Anyone have some tips on how I can redirect both to the same place that not having a forward slash sends it to?

Here's my current .htaccess code:


Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([0-9a-zA-Z+-]+)$ index.php?article=$1 [NC]
RewriteRule ^([0-9a-zA-Z+-]+)/$ index.php?article=$1 [NC]


There's also a little bit more in my .htaccess file about 404 pages, but I can confirm that doesn't effect it at all.

Thank you in advance.

abduraooft
06-21-2010, 09:40 AM
The only problem is, when I use the forward slash, it must be accepting the index.php file I direct it to as one directory further and doesn't load images and stylesheets correctly. You may solve the issue by referring all the files (css,image,js etc) with absolute paths. See the post at http://www.codingforums.com/showpost.php?p=784167&postcount=9


Anyone have some tips on how I can redirect both to the same place that not having a forward slash sends it to?I use an alternate method, which handles all the requests(except a valid reference to any files such as image,css, js) via url to the index.php page and then serve the pages based on the value of the variable $_SERVER['REQUEST_URI']

Say, my htaccess will have
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?page=$1 [L] and at the of index.php, there'll be something like

$req=split('/',$_SERVER['REQUEST_URI']);
#myprint_r($req);
if($req[1]=="")
$page_='home';
else
$_page_=$req[1];
(The above mentioned method has a side effect. You may not get the values from the $_GET global array as usually u do)

GSimpson
06-21-2010, 11:26 AM
Thank you for that excellent reply. I had a feeling there would be no escape from absolute paths, I was just trying my best to make it as simple as possible to create templates for. Oh well, can't win them all.

Also, apologies for using the wrong forum, I don't remember an apache forum being there. Used to post in PHP when I first signed up. Thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum