Forum Moderators: phranque
I don't understand regular expression so please be patient. I use mod_rewrite to avoid the ugly query string and it works just fine. There is only one problem.
If someone types:
www.mydomain.com/thedata
Instead of:
www.mydomain.com/thedata/
It will generate an error. The reason for the error is the missing slash(/) in the end. How do I fix this? I've been reading 2-3 threads on Webmaster World, but I still can't figure out what is wrong.
Here is the code in my .htaccess:
# Directory rewrite
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)/$/$1/ [R]
RewriteRule ^([^/]+)/$result.php5?domain=$1 [L]
I would really appreciate if someone could guide me a little bit.
Thanks.
-Zach
If I understand what you are trying to accomplish, then replacing your two rules with the following might work better:
# If request_filename does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if request filename plus slash does not exist as a directory
RewriteCond %{REQUEST_FILENAME}[b]/[/b] !-d
# Rewrite to script
RewriteRule ^([^/]+)[b]/?$[/b] /result.php5?domain=$1 [L]
Jim
Looks like you know your stuff :)
I did some minor changes to your code and it is very close, but there is still a minor bug.
Instead of using actual domain as a variable it uses result.php5. So the input is something like this:
result.php5?domain=result.php5
instead of:
result.php5?domain=www.test.com
I think it is a minor error, but I just can't find it.
Can you spot it?
Thanks.
-Zach
Lets take it from the beginning, here is what we want to happen.
Someone types:
www.mydomain.com/variable/
Behind de scenes the following happen:
www.mydomain.com/result.php5?domain=variable
Unfortunatly this does not work when someone types:
www.mydomain.com/variable
The changes you made seem to work, but for some reason the variable wasn't used correctly so the following happened behind the scenes:
www.mydomain.com/result.php5?domain=result.php5
So basically the input from the user isn't used at all.
Let me know if this helps, if not I will try again :)
Thanks.
# Prevent recursion
RewriteCond %{REQUEST_URI} !^/result\.php5$
# If request_filename does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if request filename plus slash does not exist as a directory
RewriteCond %{REQUEST_FILENAME}/ !-d
# Rewrite to script
RewriteRule ^([^/]+)/?$ /result.php5?domain=$1 [L]
[edited by: jdMorgan at 9:40 pm (utc) on Sep. 17, 2005]
The Internal Server Error was fixed, great.
After I put in the last piece of code it just said:
Not Found
The requested URL /result.php5 was not found on this server.
This comes up no matter what page is requested, so even if I go for the frontpage that doesn't call result.php5.
Does that help a little bit?
Thanks.
-Zach
I assumed that it would be in your 'root' directory. If not, add its path to the substitution in the RewriteRule. You can use the 'relative' path by omitting the slash, but this can result in unexpected problems as well, which is why I prefer to use a server-relative URL-path.
Jim
# encounter php4? stop
RewriteRule \.php4 - [L]
# handle /~user
RewriteCond %{SERVER_PORT} !81
RewriteCond %{REQUEST_URI} ^/~testdir
RewriteRule \.php [localhost:81%{REQUEST_URI}...] [P,L]
# handle subdomains
RewriteCond %{SERVER_PORT} !81
RewriteCond %{REQUEST_URI} \.php
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain.com$ [NC]
RewriteRule .* %{HTTP_HOST}%{REQUEST_URI} [C]
RewriteRule ^([^.]+)\. [localhost:81...] [P,L]
# catchall
RewriteCond %{SERVER_PORT} !81
RewriteRule \.php [localhost:81...] [P,L]
# End php5 rewrites
# Directory rewrite
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)/$/$1/ [R]
RewriteRule ^([^/]+)/$result.php5?domain=$1 [L]
1st Make sure everything that does not contain a .(dot) or end in a / is permanently redirected to the / version to avoid any content duplication.
RewriteCond %{REQUEST_URI} !(\.¦/$)
RewriteRule ^(.*) http://yourdomain.com/$1/ [R=301,L]
2nd Rewrite everything up to the first / that is not a true directory, does not contain a .(dot) and ends in a / to the php script. (Can be adjusted to fit your needs if you are using the script for deeper locations EG /variable/variable2/)
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/$ /~testdir/result.php5?domain=$1 [L]
/~testdir/ appears to be where the script is located -- remove or adjust if necessary.
For live use the right side of the second rule should be the full server relative (not http://yoursite.com -- canonical) path to the php script, or you will receive the same 404 error. EG If you script is in /myscripts/ you will need to make the right side of the rule /myscripts/result.php5?domain=$1
You may need to use the full canonical path for testing purposes though:
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/$ http://localhost:81/~testdir/result.php5?domain=$1 [P,L]
Let us know how this goes...
Justin
BTW The ¦ should not be a broken bar -- changed by the forum software, so you will need to adjust any copy/paste to avoid an error.
# Directory rewrite
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+) /$/$1/ [R]
RewriteRule ^([^/]+) /$result.php5?domain=$1 [L]
to this:
RewriteCond %{REQUEST_URI}!(\.¦/$)
RewriteRule ^(.*) http://localhost:81/$1/ [R=301,L]
RewriteCond %{REQUEST_URI}!-d
RewriteRule ^([^/.]+)/$ /~testdir/result.php5?domain=$1 [L]
Justin
So it goes like this in my log:
www.mydomain.com/variable/
www.mydomain.com/variable//
www.mydomain.com/variable///
www.mydomain.com/variable////
www.mydomain.com/variable/////
www.mydomain.com/variable//////
And it keeps on going like that.
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+) /$/$1/ [R]
RewriteRule ^([^/]+) /$result.php5?domain=$1 [L]
is still present in the file.
Sorry.
Justin
Maybe you should try it on another server instead of your local machine?