.htaccess rewrites my urls when I don't want it to!
I'm trying to get .htaccess to rewrite urls for my site. For example:
/Lessons/PhrasalVerbs/PVList/bringup
becomes
/index.php?page=Lessons/PhrasalVerbs/PVList/bringup
This seems to be a rela开发者_Python百科tively common desire, so after looking around the internet, I came up with (read copied bemusedly) this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.example.com/index.php?page=$1 [L,QSA]
Now as is, things work, but it's the second, not the first url, that appears in the bar. Now if I change the last line from
RewriteRule ^(.*)$ http://www.example.com/index.php?page=$1 [L,QSA]
to
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
Now the address bar looks fine, but the site goes from looking correct to losing most of its formatting and images... (unfortuntely I can't post images, or links to images...)
Also, if I click on, for example, the link to the "bring up" page again, the url goes from
/Lessons/PhrasalVerbs/PVList/bringup
to
/Lessons/PhrasalVerbs/PVList/Lessons/PhrasalVerbs/PVList/bringup
So it's sort of repeating itself.
As is probably clear, I'm completely useless at mod_rewrite and am very confused. As this seems to be a fairly common use of .htaccess, I'm hoping someone can point out my stupid mistake!
This is a pretty common mistake when doing URL rewriting and it has nothing to do with mod_rewrite.
The problem is that you’re using relative URLs in your documents. And relative URLs are resolved by the client using a base URL that is the document’s URL is not specified otherwise.
So if the document’s URL path is /foo/bar
and there is a link in it with the relative URL baz/quux
, the client resolves it to /foo/baz/quux
as baz/quux
is resolved relatively to /foo/bar
and not to /
.
You can fix this by using absolute paths instead, i.e. /baz/quux
instead of baz/quux
. Or by explicitly changing the base URI using the BASE
element. But I wouldn’t do that as it affects all relative URLs and not just relative URL paths.
精彩评论