Forum Moderators: phranque

Message Too Old, No Replies

url with .htaccess and php

         

icemancast

7:15 pm on Sep 27, 2007 (gmt 0)

10+ Year Member



here is what i am trying to do.

i have a content management system. the urls at the top look like this "index.php?pgid=23&tid=45"

i want the rewrite that url with .htaccess and php. where .htaccess looks to php file for rewrite.

php file will pull from the database and rewrite the url as such "site/folder/this-is-the-name/". kind of like the way wordpress does.

let me know if this makes sense.

any ideas.

g1smd

10:25 pm on Sep 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, this gets covered every week, often at least a couple of times.

The URL that you want the users to SEE will need to appear on the page, in the links that the user will click.

Once you have done that, you need the rewrite to be able to look at the requested URL and silently translate that into the internal filepath needed to fetch the file.

You will also need a separate 301 redirect from the dynamic URL back to the static format URL, so that the dynamic format URL can no longer be directly accessed.

Have a look at recent posts here, and pull some code. Check it very carefully. This stuff is completely unforgiving of typos. It does exactly what you program it to do, not what you thought that it might do.

icemancast

2:18 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



my friend is doing this like this:

htaccess
RewriteEngine On

RewriteRule index.html index.php [L,NC]

RewriteRule deals.html deals.php [L,NC]
RewriteRule ^deals/[a-z0-9_-]+/([0-9]+)\.html$ deals.php?op=fullstory&deal_id=$1 [L,NC]
RewriteRule ^categories/[a-z0-9_-]+/([0-9]+)\.html$ deals.php?op=dealcat&dcat_id=$1 [L,NC]

RewriteRule news.html news.php [L,NC]
RewriteRule ^news/[a-z0-9_-]+/([0-9]+)\.html$ news.php?op=fullstory&news_id=$1 [L,NC]
RewriteRule ^category/[a-z0-9_-]+/([0-9]+)\.html$ news.php?op=newscat&nc_id=$1 [L,NC]

php file

$catname = preg_replace('#[^A-Za-z0-9_-]#','-',$row2['dcat_name']);
$linkcategory = "categories/" . $catname . "/" . $dcat_id . ".html";

I guess i want to know if this is right or if there is a better way to do this. i am trying to create nice urls and search engine friendly urls. every solution i look at is different than the other and i guess that is why i get confused.

jdMorgan

9:09 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the code you posted, the mod_rewrite code at the top is "step two" in what g1smd described. The PHP preg_replace code at the bottom is "step one," and is likely similar to what you need to add to your script(s) to change the URLs that appear as links on your pages.

Jim

g1smd

10:34 pm on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am not sure why you have this in your code:

RewriteRule index.html index.php [L,NC]

It seems that it takes a request for index.html and fetches the content from index.php instead.

If content at index.php can also be directly accessed then you have Duplicate Content issues - the same content at multiple URLs.

Additionally, since you allow the requested index.html URL to have ANY case, that further promotes dozens more Duplicate Content URL combinations.

In any case, you should refer to the root of a site as just www.domain.com/ without actually mentioning what the index file filename really is. The DirectoryIndex directive takes care of what the index file filename is actually called and goes off to fetch the content from it.

If you need to retain .html URLs for your site, but now want to generate the site using PHP scripts, look to the AddType directive to set that up in a far safer manner.

icemancast

11:40 pm on Oct 1, 2007 (gmt 0)

10+ Year Member



this is the way my partner handled it to get urls as such:

domain.com/category/Comics/3.html

what i am trying to do is index.php?query=test

to be domain.com/category/this-is-name/

and i have several php files in root i need to do this for

news.php

teams.php

index.php

and i get so confused as to which of the 100 of solutions on the message board will help me. i am great at php, mysql and css but get stuck on refex and htaccess stuff... don't know why :)

g1smd

9:53 am on Oct 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Having a very clear definition of what you want to do, before you start, is very important.

You'll wander round in the dark for many months, but as you try more things, you'll find better ways to do things.

jdMorgan once said to me: "Every time you get the '500 Server Error' error message you have learnt one more way how not to do things".

The big relevations came in understanding that web URLs and internal server filepaths are two very different things and that a redirect makes the browser fetch a new URL, while a rewrite translates a requested URL into a different internal server filepath without exposing what that internal filepath actually is.