Get exclusive CAP network offers from top brands

View CAP Offers

Need help with mod-rewrite

ewhitaker asked 1 year ago
I wish I was better at regular expressions. I have searched high and low for help on this trying to figure it out on my own – now I need to ask for some help.

I have a directory called helpful_links. I am using a php script to build the pages for each category.

a page that shows like this:
/helpful_links.php?cid=38

Needs to show like this:
/helpful_links/category name.php (the cid numbers each represent a certain category)

Categories that have subpages are currently displayed as:
/helpful_links.php?pg=2&cid=38
/helpful_links.php?pg=3&cid=38

And they need to show as:
/helpful_links/2/category name.php

I need to use mod-rewrite for an .htaccess file, but I get lost with the regular expressions when it comes to assigning categories for each cid number.

Is there anyone here at CAP that is familiar with regex and mod-rewrite that can give me a hand?

So far I have come up with this:
RewriteEngine on
RewriteRule ^helpful_links/(.*)/(.*).php /helpful_links.php?pg=$1&cid=$2

I was expecting the above results to bring back this:
/helpful_links/2/38

But when I try it out, nothing changes, and no idea how to include the category names.

I’m willing to pay to get this done.

5 Answers
ewhitaker answered 1 year ago
Anybody???

Engineer answered 1 year ago
I don’t have the time to figure this out completely, but I can tell you this: If you want the category name to be in the rewritten URL, then I am pretty sure that the category name also needs to appear in the original URL. At least, that is the only way I would know how to handle it.

Looking at your rewrite code:

RewriteEngine on
RewriteRule ^helpful_links/(.*)/(.*).php /helpful_links.php?pg=$1&cid=$2

First off, I would delete the forward slash before the word “helpful.” It should look like this:

RewriteEngine on
RewriteRule ^helpful_links/(.*)/(.*).php helpful_links.php?pg=$1&cid=$2

With the above rule, this is what is actually happening….

The first (.*) is $1. The second (.*) is $2. (I think you know that part already. <span title=” title=”” class=”bbcode_smiley” /> )

If you want the URL to appear as /helpful_links/2/category-name.php

–then–

In the original URL, where you have cid=$2 — the value of $2 needs to be exactly what you want to show up in the rewritten URL. If you have a number here, then the number will be in the URL. If you have a string-of-words-like-this, then that string of words will be in the rewritten URL.

Examples:

Original URL: /helpful_links.php?pg=6&cid=7
Rewritten URL: /helpful_links/6/7.php

Original URL: /helpful_links.php?pg=2&cid=category-1-here
Rewritten URL: /helpful_links/2/category-1-here.php

Original URL: /helpful_links.php?pg=250&cid=wow-look-at-this
Rewritten URL: /helpful_links/250/wow-look-at-this.php

Hope that makes some sense. :smoker:

ewhitaker answered 1 year ago
“In the original URL, where you have cid=$2 — the value of $2 needs to be exactly what you want to show up in the rewritten URL. If you have a number here, then the number will be in the URL. If you have a string-of-words-like-this, then that string of words will be in the rewritten URL”

This, I understand, but then the script would not know what cid number should be associated with the string of words. And since each cid number is assigned a category, I was hoping to find out if there was a way to write the code to reflect this.

For example:
cid 10 = poker
cid 11 = casino

The PHP script I am using automatically assigns a cid number to each category I create in the admin panel. It just displays it in the browser as a number instead of the associated name. Perphaps there is a way in the PHP code to manipulate this?

I can live with displayed results as:
/helpful_links/2/10.php
but for the search engines, /helpful_links/2/poker.php would be 10 times better.

Without the forward slash in front of “helpful_links” how would the script know where to find the file? :tooconfus

I really do appreciate you taking the time to try to help. thank you!

Engineer answered 1 year ago
ewhitaker wrote:
Without the forward slash in front of “helpful_links” how would the script know where to find the file?

If helpful_links.php is in the main root directory (e.g. yoursite.com/helpful_links.php), then you don’t need the forward slash. (I don’t find it necessary, anyway. I don’t remember if the / messes up the rule or not… you could try it both ways, I guess).

And since each cid number is assigned a category, I was hoping to find out if there was a way to write the code to reflect this.

For example:
cid 10 = poker
cid 11 = casino

Yeah, I know what you mean…. Somewhere along the way, the query that calls for the information in your database needs to get the data associated with cid10, cid11, etc… If each cid is assigned to a unique category name, you can make this work by calling for the category name (instead of the cid number) in the query.

For example, change

$query = “SELECT * FROM tablename WHERE cid = 10”;

to

$query = “SELECT * FROM tablename WHERE categoryname = ‘poker'”;

(be careful to include the single and double quotes in the right places)

If the category name is contained in the URL, first you could “GET” the category name and then insert it into the new query, like this:

$cat_name=$_GET;

$query = “SELECT * FROM tablename WHERE categoryname = ‘$cat_name'”;

The URL where it retrieves the cid from would look like this, for example:

yoursite.com/helpful_links/2/poker.php

which, from the .htaccess file, would be rewritten to look like this behind the scenes:

yoursite.com/helpful_links.php?pg=2&cid=poker

Hopefully this works out for you. Good luck! <span title=” title=”” class=”bbcode_smiley” />

ewhitaker answered 1 year ago
Thank you Dave!

I’m going to give this a try – I’ll let you know if it works. This is my first mysql database, oh boy….