PHP/.htaccess URL Stuff

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
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

PHP/.htaccess URL Stuff

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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...
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you want http://www.mysite.com/{PAGENAME}/{ACTIO ... OTHERPARAM} to route to index.php?page=PAGENAME&ACTIONNAME=ACTIONVALUE&myownparam=OTHERPARAM
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Exactly right.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

I get a 500 server error with that. Hm.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Needs a $ at the end of the regex.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply