Newb question

New to mod_rewrite? This is a good place to start.

Newb question

Postby comprx » Wed Jul 22, 2009 7:49 pm

My site only has about 4 or 5 pages. The pages are populated with data from a database. I want to have my URLs do the following:

Code: Select all
www.mysite.com

do nothing simply go to the site

Then:
Code: Select all
www.mysite.com/categoryname

changes to
Code: Select all
www.mysite.com/page1.php?cat=categoryname


and lastly :
Code: Select all
www.mysite.com/examplecity-examplestate/exampleuid

changes to
Code: Select all
www.mysite.com/page2.php?city=examplecity&state=examplestate&uid=exampleuid


These are the only rewrites i need as my entire site could function with just those.

I read the tutorials and sort of grasped how to do the redirects, but I'm not quite there.

I almost forgot. I need it to do that and still show what the user typed in.
so
Code: Select all
www.mysite.com/category

is changed to
Code: Select all
www.mysite.com/page1?cat=category

but the user still sees
Code: Select all
www.mysite.com/category
in the address bar
comprx
 
Posts: 6
Joined: Wed Jul 22, 2009 7:38 pm

Postby richardk » Thu Jul 23, 2009 9:25 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
# /categoryname --> /page1.php?cat=categoryname
RewriteRule ^([^/]+)/?$ /page1.php?cat=$1 [QSA,L]

# /examplecity-examplestate/exampleuid --> /page2.php?city=examplecity&state=examplestate&uid=exampleuid
RewriteRule ^([^/]+)-([^/]+)/([0-9]+)/?$ /page2.php?city=$1&state=$2&uid=$3 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Looks good

Postby comprx » Thu Jul 23, 2009 9:32 am

Ok that looks like it should work but the portion you wrote for UID looks like it will only take numbers.

How would i write that to include letters AND hyphens?
Code: Select all
# /examplecity-examplestate/exampleuid --> /page2.php?city=examplecity&state=examplestate&uid=exampleuid
RewriteRule ^([^/]+)-([^/]+)/([0-9]+)/?$ /page2.php?city=$1&state=$2&uid=$3 [QSA,L]


a user id has the possibility of being any combination of #s, letters and "-" (hyphens)

other that that this looks amazing thank you![/code][/quote]
comprx
 
Posts: 6
Joined: Wed Jul 22, 2009 7:38 pm

Postby richardk » Thu Jul 23, 2009 9:33 am

Then replace "0-9" with "^/".
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby comprx » Thu Jul 23, 2009 9:35 am

Thank you SO MUCH
comprx
 
Posts: 6
Joined: Wed Jul 22, 2009 7:38 pm

Slight problem

Postby comprx » Sun Jul 26, 2009 1:02 am

Ok I attempted to use this wonderful .htaccess tonight and started to run into issues.

Issue 1: it didn't work (got a blank page with every attempt)
Issue 2: It seems to be messing with something in my paths related to www.mysite.com and mysite.com (the latter wouldn't resolve all of the correct filepaths for my images.?

I DID alter it to accomidate my revised need though so maybe it was my fault (and I am sure it is) my revision is as follows:

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
# /location --> /index.php?loc=location
RewriteRule ^([^/]+)/?$ index.php?loc=$1 [QSA,L]

# /examplelocation/exampleuid --> /index.php?loc=exampleloc&uid=exampleuid
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?loc=$1&uid=$2 [QSA,L]
comprx
 
Posts: 6
Joined: Wed Jul 22, 2009 7:38 pm

Postby richardk » Sun Jul 26, 2009 5:18 am

Issue 2: It seems to be messing with something in my paths related to www.example.com and example.com (the latter wouldn't resolve all of the correct filepaths for my images.?

The second rule might be matching those files and/or relative paths might be broken. FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Ignore requests to existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
# Ignore requests to existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
# /examplelocation--> /index.php?loc=examplelocation or
# /examplelocation/exampleuid --> /index.php?loc=exampleloc&uid=exampleuid
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ index.php?loc=$1&uid=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Ok we are getting farther

Postby comprx » Wed Jul 29, 2009 9:49 pm

ok this is working for the FIRST variable now (loc) but the second portion (uid) is failing.

it is passing the variable correctly but when my scrip uses that variable and creates the page accordingly it doesn't seem to be loading certain things based off of some paths.

it is not loading the style sheets or images of which the paths are listed in the code in the following format: "images/someimage.jpg" or "styles/style.css" this is kinda odd considering if i pass it only one paramiter (loc) it loads them all just fine. The only difference in my script between passing loc and loc+uid is that the first php includes one file, and the second php includes a different one instead.

I hope that makes sense.
The problem is almost solved just some path issues to sort out when passing the second variable (again that one is uid)

any new ideas?

EDIT:
Ok upon looking back at your previous post I realize that it is a path problem. I don't like the idea of #1 on there and while #2 seems ok it would entail the same amount of work as #1 initially and i don't honestly know where i would find the config file you mentioned. 3 and 4 are not for me either. so it looks like 1 or 2 are the only options. Can you tell me reall fast where/how to edit the global include/configuration (I use PHP a ton on this site so if i knew where this file was this option doesn't seem too bad)
comprx
 
Posts: 6
Joined: Wed Jul 22, 2009 7:38 pm

Postby richardk » Thu Jul 30, 2009 3:46 pm

Can you tell me reall fast where/how to edit the global include/configuration (I use PHP a ton on this site so if i knew where this file was this option doesn't seem too bad)

For #2 you have to find every link (or PHP statement that creates a link) and add the base constant (that you create at the beginning of your file or in a global include file if you have one) and make the paths relative to it.

So you would add
Code: Select all
define('MOD_REWRITE_BASE_PATH', 'http://www.example.com/base/path/');

and find the links, eg.
Code: Select all
<a href="old/relative/path">foo</a>

and change them to
Code: Select all
<a href="<?php echo MOD_REWRITE_BASE_PATH; ?>old/relative/path">foo</a>


3 and 4 are not for me either.

What's wrong with #4?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

4 has issues

Postby comprx » Fri Jul 31, 2009 7:40 pm

number four has issues with IE and unfortunately I will have clients that use it as far back as IE6.

the issue i am seeing now is in reference to css background images not showing up. I am trying to figure out a way to inject the root through php into the css sheets


EDIT:
Ok after playing with some stuff I used the global variable option with full success. thanks
comprx
 
Posts: 6
Joined: Wed Jul 22, 2009 7:38 pm

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: Google [Bot] and 23 guests

cron