Forum Moderators: phranque

Message Too Old, No Replies

PHP GET andMod rewrite

GET method not retreving Value from URL

         

ShadowPhantom000

10:28 am on Sep 15, 2010 (gmt 0)

10+ Year Member



Im working on a Web comic and Im trying to Mod_rewrite all my links to the format

domain.net/comic/0001
and
domain.net/production/comic/0001


from the format:

domain.net/comic.php?id=0001
domain,net/production/comic.php?id=0001


where 0001 is the ID number for the specific comic, and the production folder is for working on the new site.

so My mod_rewrite rule is


RewriteRule comic/(.*)$ comic.php?id=$1
RewriteRule comic/(.*)/$ comic.php?id=$1


The Page seems to display alright but the php $comicId = $_GET['id'];
isent geting the value at all. I figure its a problem with my Rule but I dont see whats wrong... Ive tried all kinds of variants too. If someone can just help me Id really appreciate it.

jdMorgan

5:18 pm on Sep 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rules need an [L] flag on the end, especially if there are any rules after them.

They can also be easily combined into a single more-specific rule:

RewriteRule ^(production/)?comic/([^/]+)/?$ comic.php?id=$2 [L]

A couple of additional points here to clarify...

1) You cannot "rewrite URLs." If you want the links like domain.net/production/comic/0001 to appear on your HTML pages, then edit those pages (or the script that produces those pages) to link to URLs in that format. Then internally rewrite those requested URLs to the correct script filepath, as in the code you posted.

The on-page changes actually "change" the URLs, and the mod_rewrite rule then connects requests for those new URLs to the script, so that content can be served.

2) Assuming that you've already got mod_rewrite set up and enabled, if you request the URL http://example.com/comic/0001 from your server, then content should be delivered by comic.php for a GET value of "id=001".

If that's how you're testing, and the first point above is understood, then try dumping all the PHP variables to see if the id got 'lost' somewhere. This can happen if the result of the missing [L] flag is to move the QUERY_STRING variable value to REDIRECT_QUERY_STRING.

Also, consider that the script itself could retrieve the id directly from the originally-requested URI, instead of form the 'copied' query string.

Jim

ShadowPhantom000

10:33 am on Sep 16, 2010 (gmt 0)

10+ Year Member



Sorry to seem Oblivious, I get that mod_rewrite dosent change the Links.

I tested to make sure that mod_rewrite is enabled with

RewriteRule ^a.html$ b.html


when I go to domain.net/a.html it pulls the content for b.html. so I know that its enabled

what im trying to do is change
http://example.com/comic.php?id=0001
to
http://example.com/comic/0001

all I have in the php right now is

$id = $_GET['id'];
echo $id;

when I go to comic.php?id=0001 it prints 0001
but when I go to comic/0001 it displays nothing

When I print_r($GLOBALS); it says that id is undefined. meaning the Rule Isent working...

For simplicity's sake, I wont use the rewite Rule in the production(im fine typing in the?id=001)

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^comic/([^/]+)/?$ comic.php?id=$1 [L]

should work, also if I put this same line in a different .htaccess file in the production folder would that work?

jdMorgan

3:33 pm on Sep 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your a.html to b.html rewrite works, then the problem is either one of code location or of interference from another rule or even another Apache module.

Please state explicitly *where* you have put this code -- Where is this .htaccess file located in your server's filespace?

Jim

ShadowPhantom000

9:12 am on Sep 22, 2010 (gmt 0)

10+ Year Member



I have it in the /var/www/ Folder, I had one in the var/www/production folder but I emptied it.

I dont believe I have anything else in any other apache module. Ill check, where else should I look? but I don't think I made any changes to anything else.

g1smd

10:17 am on Sep 22, 2010 (gmt 0)

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



Make sure you have cleared the browser cache before testing again.

ShadowPhantom000

7:02 am on Sep 23, 2010 (gmt 0)

10+ Year Member



Ok I got somthing working... not exactaly what I want... but its halfway there.

i changed the Rule to

RewriteRule ^([^/]+)/?$ /production/comic.php?id=$1 [L]

and its now showing the home page as comic.php and is getting the Number.

g1smd

9:24 am on Sep 23, 2010 (gmt 0)

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



Could you explain that in more detail, as your wording is ambiguous?

What is the URL you type in?

What is the URL that is displayed in the browser bar AFTER you have pressed "enter"?

ShadowPhantom000

3:00 am on Sep 24, 2010 (gmt 0)

10+ Year Member



Sorry haha, I set the RewriteRule to the above and now it can be entered as

tll.net/0001

which is actualy

tll.net/production/comic.php?id=0001

but after some tweaking I realized how to fix it, then changed the Location of the comic.php file for organization

tll.net/comic/0001
now becomes
tll.net/comic/comic.php?id=0001
using
RewriteRule ^comic/([^/]+)/?$ /comic/comic.php?id=$1 [L]

last step Im making is to only alow numbers since the ID will only be numerical.