Debian Linux 7.0 Apache2 Configuration: 301 Redirects and Directory Permissions
With nothing to do one day, I registered three domain names (kitaluft.com, 0x97.com, jourman.com), but only one personal blog, so at the beginning I just casual
With nothing to do one day, I registered three domain names (kitaluft.com, 0x97.com, jourman.com), but only one personal blog, so at the beginning I just casually pointed all three domains to the same hosting space without doing any configuration, which meant the file listing was visible when accessing certain directories. Later I designated just one URL — "blog.kitaluft.com" — and redirected the others to it via apache2. That method was extremely simple: just add "Redirect / http://blog.kitaluft.com" to the configuration file.
But this obviously has a problem. Say Baidu has indexed one of my articles with the URL "http://www.kitaluft.com/id=8?"; after applying the above redirect, clicking that link won't take the user to that article. A 301 redirect, however, can solve this problem.
To borrow a sentence from the blogger at "http://www.admin5.com/article/20140717/552719.shtml", there are two ways to implement a 301 redirect:
- When a.com redirects to b.com, accessing a.com/1.html also redirects to b.com
- When a.com redirects to b.com, accessing a.com/1.html redirects to b.com/1.html
Clearly, the second method has the advantage. This article describes how to make "www.kitaluft.com" redirect to "blog.kitaluft.com". The process requires modifying the configuration file that was previously written for this blog as the virtual host configuration. The original text was:
<VirtualHost *:80>
ServerAdmin master@0x97.com
ServerName www.kitaluft.com
Redirect / # explicit redirect
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Modify to:
<VirtualHost *:80>
ServerAdmin master@0x97.com
ServerName www.kitaluft.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.kitaluft.com
RewriteRule ^(.*)$ http://beyan.me/$1 [R=permanent,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Enable apache2 rewrite module
Bash
# cd /etc/apache2/mods-enabled
# ln -s ../mods-available/rewrite.load ./
Set directory permissions
<Directory /var/www/>
Options FollowSymLinks MultiViews # originally had Indexes, which would list files in a directory when accessed
AllowOverride None
DirectoryIndex login.php index.php
Order allow,deny # allow first then deny — like a blacklist, allow all by default and deny specific ones
allow from all
Deny from 183.60.243.234 # block this IP from accessing the site — it attacked 37 times in one day, recommend site owners blacklist it
</Directory>
Set file permissions
<Files test.html>
Order allow,deny # Deny access to this file in any directory of the site
Deny from all
</Files>
Set network namespace permissions
<Location /test>
Order Allow,Deny
Deny from all
</Location>
This will block access to any URL starting with /test, for example: http://www.kitaluft.com/test http://www.kitaluft.com/test/abc.html http://www.kitaluft.com/test/test/abc.html For directory permissions, see: http://www.php100.com/html/webkaifa/apache/2009/0418/1193.html Postscript: I set up a 301 redirect, and after it took effect the URL ended up with two slashes, e.g. "http://beyan.me//". It doesn't actually affect functionality, but something must clearly be wrong. After a lot of searching through Baidu and Google, here are the lessons I learned: 1. In the line below, ^(.*)$ works like any other regular expression — it means any line including blank lines, except newline characters. 2. If you remove the "http://" from the second argument, the browser address bar will get stuck in an infinite loop. 3. Removing the "/" before $ prevents the double "//" error, probably because the "$1" variable itself already starts with "/". 4. There must be no spaces inside the "[R=permanent,L]" flag, or the apache2 service will report an error. 5. "[NC]" means case-insensitive.
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$ http://beyan.me/$1 [R=permanent,L]
评论Comments
加载中…Loading…
留下评论Leave a comment