Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite with dynamic php urls?

This is getting really confusing.

         

Fourjays

12:43 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



Hi,
I have spent the last 6 hours reading up various tutorials, and forum threads, etc on how to do mod_rewrite, and I still can't manage to make it work. I know people have asked this a million times here, but I am finding many conflicting statements in my searches.

I have made a simple script, called test.php, which takes a value, and displays it (just for testing that I can get friendly urls working). So for example mysite.com/test.php?a=blah will echo "a is blah". I also have an else so that it will say it isn't.

So I want to have it so that when the url is mysite.com/test/a/ it will display the equivalent of mysite.com/test.php?a=blah.

I have tried numerous values and settings in my .htaccess, but I get the same result everytime - it won't recognise the setting of a. I have read so many tutorials on this now, and several of the threads on here, but still can't make sense of it. What am I missing?

Here are two examples of what I have tried in .htaccess:


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/test$ /test.php
RewriteRule ^/test/([0-9]+)$ /test.php?a=$1
</IfModule>


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/test$ /test.php
RewriteRule ^/test/(var1)$ /test.php?a=$1
</IfModule>

I also found this on a tutorial here, but I don't really understand whether this is the right thing, or how to apply it to my situation:


<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/test$ /test.php
RewriteCond %{CONDITION_STUFF} ^(var1)/no-var/(var2)
RewriteRule ^no-var/no-var/no-var$ /%{CONDITION_STUFF}-to-use-variables-type-%1-and-%2
</IfModule>

The eventual aim is to convert a script I am developing for a site, to use friendly urls, which have urls along the lines of index.php?q=screenshots&catid=3 at the moment.

For something that is portrayed as being oh-so-simple it is oh-so-confusing.
Thanks.

PS: Just thought of something... Does it make any difference if the script is on a sub-domain? I currently have the script on a sub-domain for testing purposes.

jdMorgan

2:21 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fourjays,

Welcome to WebmasterWorld!

Be careful of your patterns and pattern-anchoring...

Also, be aware that in an .htaccess context, the current directory's prefix is stripped and will not appear in the path that the RewriteRule tests against the pattern. That is, the path 'seen' by RewriteRule is localized to the directory in which that RewriteRule resides.; The path to the current directory including the leading slash will be stripped from the URL-path tested by RewriteRule.

So, in short:


RewriteRule ^/test$ /test.php
RewriteRule ^/test/([0-9]+)$ /test.php?a=$1

won't work in .htaccess, even though it might be correct for use in httpd.conf.

RewriteRule ^test/?$ /test.php [L]
RewriteRule ^test/([0-9]+)/?$ /test.php?a=$1 [l]

should work better.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

Fourjays

4:02 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



Thanks for the reply. Thats the bit that is making it so confusing - some information refers to both httpd.conf and .htaccess

I understand that bit better now, although it still isn't working right. I don't know which way you would put it, but the numerical value entered still isn't being understood by the php.

I put the url as test/1/ in the browsers address bar, which should deliver the same result as if I put in test.php?a=1. However, I get a php error stating that "a" is an undefined index. Am I missing an important line in php or .htaccess that makes php read the value of "a" under mod_rewrite?

[webmasterworld.com...] Is it anything to do with the references to "(var1)" on that information?

Thanks again.

PS: I just tried using (var1), but without success.

jdMorgan

9:39 pm on Feb 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, those var1, var2 names in that post are 'symbolic' in that they stand for whatever variable you might want to match and back-reference.

You could try:


RewriteRule ^test/([0-9]+)/?$ /test.php?a=$1 [PT,L]

to put mod_rewrite in pass-through mode. That sometimes helps when passing the result of mod_rewrite to another module (in this case, php) for further processing. Mod_rewrite has two modes: It can act as a URL-to-filename translator, which is it's default mode, or it can act as a URL-to-URL translator, the mode it uses if the [PT], [P], or [R] flags are used.

If you are on an Apache 1.x server, and you have any question that it might not be configured absolutely correctly, you might want to check the httpd.conf file and make sure that php appears in the LoadModule list *before* mod_rewrite. Modules are processed in the reverse order that they appear in the LoadModule list under Apache 1.x; If php appears after mod_rewrite, then mod_rewrite can't pass requests to php, and direct php file requests will by-pass mod_rewrite - neither of which is good. This does not apply to Apache 2.x

Jim

Fourjays

10:58 pm on Feb 3, 2006 (gmt 0)

10+ Year Member



Well I just tried that as well, and it won't work either. :-/

I'm not sure about the Apache version (Im on a shared host), but I believe it is 2. Any sure way to tell?

At the moment, it looks like Im going to have to go to plan b, which is using path_info. Ive tested and tried this, and it works fine - just means I will have to recode lots of parts of my script to take it, which is why I would have preferred mod_rewrite - less stuff would need changing.

Unless there are anymore ideas, I'm gonna have to get off my lazy bum, and start coding. :P

Thanks for the help anyway.

vbsaltydog

3:08 am on Feb 5, 2006 (gmt 0)

10+ Year Member



Have you tried using the mod rewrite wizard yet?

<snip>

[edited by: jdMorgan at 3:39 am (utc) on Feb. 5, 2006]
[edit reason] No URLs, please. See Terms of service. [/edit]