Forum Moderators: phranque
once upon a time i've read this article:
[#*$!.com...]
i thought it's great and did the same with my PHP projects - all requests were redirected to the main index file (i was testing it on my localhost machine).
finally it came time to test this on the remote server. i uploaded the file structure, and everything was fine, except one thing:
when i entered the main URL without the trailing slash, it would return me an error (400 bad request), with the trailing slash it was all good.
this isn't a big deal but users tend to forget about trailing slash when entering a URL, and all they were getting was the error.
unfortunately, after reading apache docs and rewrite guide, i couldn't come up with a set of rules that would prevent this behavior.
this is the big picture:
-----------
/ - root folder: no .htaccess file
/int - there is .htaccess file with these rules:
RewriteEngine on
RewriteBase /int
RewriteRule .* index.php
------------
these settings work fine on my localhost (Apache 2), but when uploaded to the server (Apache 1.3 i guess) there is no way that when i enter the URL with no trailing slash (here: [mysite.com...] it won't return me an error.
i tried applying the trick from apache rewrite guide, no effect. i tried various other rules found wherever on the net - no effect..
the only thing i noticed is that this works when i remove the last rule.
is there anyone who could help me with this? how to make the URL work without the trailing slash?
any help is greatly appreciated..
peace
Welcome to WebmasterWorld.
RewriteEngine on
RewriteBase /int
RewriteRule .* index.php
There are a couple of issues I see with your code:
1. there is nothing to stop a loop if index.php is requested.
2. there is no indication of the last [L] to indicate this is the last rule. (not necessarily critical with only one rule, but still good coding.)
The following code should *visibly* redirect a request without a trailing / to a /. The reason the visual portion is important, is it also sends a 301, 'permanently moved' header to the Search Engines, so they will not index the slash and non-slash version, which can cause a duplicate content issue.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^([^.]+)$ /$1/ [R=301,L]
! = NOT
$ = 'at the end of the line'
() = create a variable
[^.]+ = 1 or more characters that are not .(dot). By using the $ at the end of the line, the rule will fail and eliminate the need to check for a .(dot) in a second condition if there is a .(dot) in the requested URL, while giving the ability to use a 'forward looking' regular expression.
Condition: Checks to see if the URL requested ends in a / and if it does, the rule will fail and there will be no rewrite.
Left Side Rule (up to the $): Store the full path requested in a variable, if the path does not contain a .(dot).
Right Side Rule (after the $): If the condition, or the negative regular expression have not caused the rewrite to fail, take the full path stored, and return to the same place, with a / added after the variable (which is the file path).
Hope this helps.
Justin
thanks a lot for the explanation.. i'm really new to the mod_rewrite issues, and i really appreciate your post.. im giving it a careful read now.
peace
ps. i dont know if i may point to the article that i was talking about in the first post, but since it's a well known site i really respect the authors' knowledge there.. i would also like to get a feedback on what you think of it.. do a search in the famous ;) engine for "succeed urls" and it's the first site that shows up. i'm posting this for reference purposes only (not promotional nor anything else..)
here's what i've done:
-- 1.
/ - root of the public html - i placed an .htaccess file there with your rules:
RewriteEngine on
RewriteCond %{REQUEST_URI}!/$
RewriteRule ^([^.]+)$ /$1/ [R=301,L]
-- 2.
/int/ - the directory - i placed an .htaccess file there with my rules:
RewriteEngine ON
RewriteBase /int
RewriteRule .* index.php [L]
(i added the [L] as you suggested but i'm still not sure if this is semantically correct)
---
now this is still causing the remote server to give me an 400 bad request error when i enter the url [mysite.com...] with no trailing slash. (like i said it's working fine on localhost)..
something must be wrong with my rules in /int/ directory - all i want is this rule to redirect all requests to /int/index.php ...
..
sorry for posting this again.. but i really don't know what could be wrong.. could this be something with the server at my hosting provider or it's the rules?
thanks!
To make the redirect you would like to happen, you will need to exclude the index.php file from qualifying for or matching the rule. As it is now the index.php file will be rewritten to the index.php file over and over and over and...
The correction for this in the /int/ directory would be:
RewriteEngine ON
RewriteRule !^index\.php$ /int/index.php [L]
Remember the preceding / on the right side of the rule is necessary in the .htaccess file, but not in the httpd.conf file.
You *may* need to add the first code that redirects requests without a / to a request with a / in the specific directory. This will depend on the setting of your server's inherit option. Try without, but if a non-slashed request does not redirect, you will need to add it or change the options.
If you elect (or need) to add it, your final file in the /int/ directory would look like this:
RewriteEngine ON
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^([^.]+)$ /$1/ [R=301,L]
RewriteRule !^index\.php$ /int/index.php [L]
And your root .htaccess would look like this:
RewriteEngine ON
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^([^.]+)$ /$1/ [R=301,L]
Justin
i installed apache 1.3.33 to have the same environment as on the server.. and the rules that you gave me in the first place are not even needed - apache handles the url properly.. which means that my rules were completely wrong..
thank you very much again for your help.
i will be visiting this forum, maybe other topics, but hopefully i'll help someone.
have a nice weekend!