Rewriting URLs via .htaccess on a shared hosting environment
My college gave me a free blog that has restrictive options. It's Moveable Type and I'm not able to edit the URL through the MT interface.
They suggest I put an index.html page up that does a meta red开发者_如何学编程irect to the full URL of my blog. Instead, I'd like to mask it using mod_rewrite in an .htaccess file.
How can I change this URL:
http://www.personal.mycollege.edu/myuserid/blogs/my_blog/
To just:
http://www.personal.mycollege.edu/myuserid/
Also, how can I get it to be persistent over articles, etc, throughout the site?
To rewrite (internal redirect) /myuserid/
to /myuserid/blogs/my_blog/
while having this rule in .htaccess located in /myuserid/
folder, use this rule:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^$ blogs/my_blog/ [L]
If you add this rule just below the above one, you should rewrite articles etc as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!blogs/my_blog/)(.+)$ blogs/my_blog/$1 [L]
The above rule will only rewrite non-existing URLs (I think it is a correct approach -- if you think otherwise just remove 2 RewriteCond lines). This means that if /myuserid/hello.png
is requested and file does exist it will NOT be rewritten to /myuserid/blogs/my_blog/hello.png
.
In theory you can combine these 2 rules into a single one (by replacing (.+)
by (.*)
), but for some strange reason it does not work (or work incorrectly) on some servers. In any case -- here it is:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!blogs/my_blog/)(.*)$ blogs/my_blog/$1 [L]
Also, how can I get it to be persistent over articles, etc, throughout the site?
This is very specific question and without knowing a lot of details on how it generates the links etc I cannot give you a definite answer to this.
It is possible that using some sort of ReverseProxy will do this job -- instead of just rewrite use internal proxy thing -- it will be completely transparent to users (same as rewrite) but should be able to fix the links on the page from real to nice ones. Unfortunately my knowledge on this particular subject is very limited to give you any real example.
精彩评论