Page 1 of 1
PHP/.htaccess URL Stuff
Posted: Sat Dec 01, 2007 1:25 pm
by waradmin
This does contain php so I am asking it here, not sure where else to ask it. If this is the wrong board, I really do apologize in advance.
My PHP script uses URLS like:
Code: Select all
index.php?page=members&do=add
index.php?page=about
index.php?page=get&code=9089fsd90fsdf
What I am looking to do is shorten the URLS using htaccess, but I am lost as how to do it. I have tried a few different .htaccess approaches but they have not treated me well.
The closest .htaccess to what I need is
Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteRule index/(.*)/(.*)/(.*)/(.*)/$ /v2/index.php?$1=$2&$3=$4
Which generates a URL like
Code: Select all
http://www.SomeSite.com/index/page/get/code/0984590348590/
What I am looking to do is strip the index and page part out of the address (ie no /index/page/) however when I modify the htaccess to look like this:
Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/(.*)/(.*)/(.*)/$ /v2/index.php?$1=$2&$3=$4
I just get the page cannot be displayed. Any idea how to change the htaccess to allow for the short URLs like I am looking for (ie something like):
Code: Select all
http://www.SomeSite.com/get/code/0984590348590/
Note that none of the above examples have worked for me.
Thanks ahead of time for the help!
Posted: Sat Dec 01, 2007 3:23 pm
by RobertGonzalez
If you could, please explain what the URL should look like and where it will route to. There are numerous ways to do this with .htaccess. It is just a matter of getting the regex right.
Posted: Mon Dec 03, 2007 10:14 am
by waradmin
Example URL:
Code: Select all
www.somesite.com/index.php?page=get&code=9234803284&verifyrs=1
Desired URL:
Code: Select all
www.somesite.com/get/code/9234803284/1
All URL's on the site begin with:
Code: Select all
www.somesite.com/index.php?page=PAGENAME...
Posted: Mon Dec 03, 2007 11:50 am
by RobertGonzalez
So you want
http://www.mysite.com/{PAGENAME}/{ACTIO ... OTHERPARAM} to route to index.php?page=PAGENAME&ACTIONNAME=ACTIONVALUE&myownparam=OTHERPARAM
Posted: Mon Dec 03, 2007 3:02 pm
by waradmin
Exactly right.
Posted: Mon Dec 03, 2007 5:09 pm
by RobertGonzalez
// Sorry if this comes in late, I had to run to lunch before I had a chance to post this
Maybe something like:
Code: Select all
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+) /index.php?page=$1&$2=$3&myownparam=$4
Posted: Mon Dec 03, 2007 9:19 pm
by waradmin
I get a 500 server error with that. Hm.
Posted: Mon Dec 03, 2007 10:58 pm
by s.dot
Needs a $ at the end of the regex.
Posted: Tue Dec 04, 2007 9:33 am
by RobertGonzalez
scottayy wrote:Needs a $ at the end of the regex.
Sorry, forgot about that.
Code: Select all
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)$ /index.php?page=$1&$2=$3&myownparam=$4
Posted: Tue Dec 04, 2007 12:22 pm
by waradmin
Not sure why but its still giving me 500's. I did have a different htaccess file that was like:
Code: Select all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?page=get&code=$1 [L]
And that works for just the get page with code in the URL, not sure why Everah's isn't working.
Posted: Tue Dec 04, 2007 12:28 pm
by RobertGonzalez
Try this:
Code: Select all
# Turn the rewrite engine on
RewriteEngine On
# Make every request on the server go to index.php
RewriteRule ^(.*)$ index.php [QSA,L]
This is for testing. Once this is on your server, go to
http://www.mysite.com/some/ridiculous/u ... ll/rewrite and see if you still get the index.php page loaded. In fact, to test what is happening, I would suggest you create an index.php file that looks like this:
Code: Select all
<?php
echo "This is the index page in {$_SERVER['HTTP_HOST']}.\n";
echo "<br />And the request is {$_SERVER['REQUEST_URI']}.\n";
?>
So it will tell you what the URI is and what the host is. These of course are only tests and should not be run on your production but only on your dev server.