Forum Moderators: phranque
I had a problem with my server since today morning, and perhaps somebody might help me.
I am trying to set up a syntax like
http://www.example.com/index/3/
which would lead to
http://www.example.com/index.php?a=3
if there would be more values - like :
http://www.example.com/index/3/4/
- it should change it automatically to
http://www.example.com/index.php?a=3&b=4
I have also tried to get rid of the trailing slash.
My first .htaccess looked like this :
Options +FollowSymLinks
RewriteEngine onRewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ - [L]
RewriteBase /var/www/html/~admin19/
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2&b=$3&c=$4 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) /~admin19/$1.php?a=$2&b=$3&c=$4 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2&b=$3 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) /~admin19/$1.php?a=$2&b=$3 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) /~admin19/$1.php?a=$2 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/$ /~admin19/$1.php [NE]
This caused some overload when I tried to open a php file? ( like www.xx.com/index.php , but worked fine for www.xx.com/index/ )
wel my current one looks like following :
Options +FollowSymLinks
RewriteEngine onRewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ - [L]
RewriteBase /var/www/html/~admin19/
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2&b=$3&c=$4 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2&b=$3 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/$ /~admin19/$1.php [NE]
Perhaps somebody could tell me if there are any risks, or if somehow i could make it better. Also I have now the trailing slash problem ( there HAS to be a / after the last variable )
Cheers, Markus
If your code works without this line:
Options +FollowSymLinks
I don't really know why you have this:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ - [L]
I don't think that the server choked on a request like www.xx.com/index.php when you used your first .htaccess. I think it choked on something like www.xx.com/3/index.php. The reason that would happen would be because this line would be invoked:
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) /~admin19/$1.php?a=$2 [NE]
I'm actually surprised that your code worked, because your RewriteRule patterns begin with:
^/
To solve the trailing slash problem, you would just need to check to see if there was a trailing slash at the end of the requested file or directory, so I would add a ? before the end anchor. For example, this line in your second .htaccess:
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ /~admin19/$1.php?a=$2 [NE]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2 [NE]
Here are some references that might be helpful:
mod_rewrite [httpd.apache.org]
Regular expressions [etext.lib.virginia.edu]
I agree with closed about the first rule doing nothing, and the solution to the missing trailing slash problem.
Also, the [NE] flag is only necessary if you intend to use the literal characters "%", "$", ",", or ";" in the substitution (the new URL). From your examples, it does not look like you need to use it here.
In addition, there is no need in this case to continue rewriting once a rule is matched and a substitution is made. Adding the [L] flag to the rules will speed up processing by stopping the rewrite engine after the first match is found and the rewrite takes place:
RewriteBase /var/www/html/~admin19/
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2&b=$3&c=$4 [L]
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2&b=$3 [L]
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2 [L]
RewriteRule ^([0-9a-zA-Z]+)/?$ /~admin19/$1.php [L]
Jim
thanks for your replies.
To be honest I kinda took some examples and experimented around. The first statements should check if $1 is a folder ( and thatfor it has to be used as a folder, instead as $1.php )
And I think the Rewritebase is useful, since I am not checking for the source folder in the rewriterule, but for the destination folder
RewriteRule ^/([0-9a-zA-Z]+)/$ /~admin19/$1.php
So thanks for your hints, I will try now some of those and then I'll post my full .htaccess file again :)
Bye, Markus
EDIT :
That's my current .htaccess
Did I miss something? And - yes, it worked without Options +FollowSymLinks , what's so bad about it?
Options +FollowSymLinks
RewriteEngine onRewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ - [L]
RewriteBase /var/www/html/~admin19/
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2&b=$3&c=$4 [L]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2&b=$3 [L]
RewriteRule ^/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/?$ /~admin19/$1.php?a=$2 [L]
RewriteRule ^/([0-9a-zA-Z]+)/?$ /~admin19/$1.php [L]
As closed pointed out above, in .htaccess, a RewriteRule pattern that starts with "^/" is not going to match - the Rule will never be invoked. This is a difference between RewriteRules in .htaccess, and RewriteRules in httpd.conf; Rule patterns in .htaccess must not start with "^/" because the "/" is removed by the server before the pattern in the Rule is tested.
These are equivalent, depending on what file they are in:
In .htaccess :
RewriteRule ^myfile\.html$ /otherfile.html [L]
In httpd.conf :
RewriteRule ^/myfile\.html$ /otherfile.html [L]
In either context - httpd.conf or .htaccess, when testing for the same file in a RewriteCond, you would use:
RewriteCond %{REQUEST_URI} ^/myfile\.html$
Jim
alright, I understand your point, but is it normal that it works like that from a .htaccess file?
I mean - it works - I open and link already files with
[example.com...] and also with [example.com...]
Is it normal that it works, and if - should I let it be like that, or is there something "bad" about it?
Cheers Markus
[edited by: DaveAtIFG at 10:40 pm (utc) on Oct. 19, 2003]
[edit reason] Generalized URLs [/edit]
It is not normal, but it may just be that you have a trailing slash on the path in your RewriteBase directive. I notice that the example in the mod_rewrite documentation does not include a trailing slash.
Jim