Forum Moderators: phranque
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.
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
RewriteRule ^test/?$ /test.php [L]
RewriteRule ^test/([0-9]+)/?$ /test.php?a=$1 [l]
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
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.
You could try:
RewriteRule ^test/([0-9]+)/?$ /test.php?a=$1 [PT,L]
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
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.