What is Rewrite pseudo-static?
When our website is a dynamic page (with the back-end interactive pages), rather than the real HTML static, in order to more conducive to SEO, we can consider to use Rewrite pseudo static, pseudo static characteristics from form to see is a static address, but on the server side, not the static pages, is actually made up of dynamic page to process your request, Here’s an example:
Our website link http://localhost/news.php? id=100
In the browser, we want http://localhost/news-100.html, which is not only better for SEO optimization, but also cleaner for the user.
Typical.htaccess files:
# enable URL rewriting
RewriteEngine on
Scope for URL rewriting
RewriteBase /path/url
# What conditions are metRewriteCond %{HTTP_HOST} ! ^www\.oy51\.com$ [NC]What rules apply
RewriteRule .? http://www.oy51.com%{REQUEST_URI} [R=301,L]
Copy the code
RewriteCond starts with a % because {HTTP_HOST} is an Apache variable and needs to be indicated by %. From! The beginning is the condition of the match, supporting the re. ! If HTTP_HOST is not www.oy51.com. [NC](no case)
- [L](last) : Terminates a series of Rewriteconds and RewriteRule
- [R] : trigger a redirect. You can specify the redirect type, e.g. [R=301]
- [F](forbidden) : Forbidden to view a specific file, Apache will trigger 403 error
1. The primary domain name points to a subdirectory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?oy51.com$
RewriteCond %{REQUEST_URI}! ^/subfolder/ RewriteCond %{REQUEST_FILENAME}! -f RewriteCond %{REQUEST_FILENAME}! -d RewriteRule ^(.*)$ /subfolder/The $1
RewriteCond %{HTTP_HOST} ^(www.)?oy51.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
Copy the code
2. Standardize WWW
①, all unified to take WWW, such as oy51.com unified to www.oy51.com
ewriteEngine on
RewriteCond %{HTTP_HOST} ^oy51\.com$ [NC]
RewriteRule ^(. *)$ http://www.oy51.com/$1 [L,R=301]
Copy the code
②, all unified to do not take WWW, such as www.oy51.com unified to oy51.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.oy51\.com$ [NC]
RewriteRule ^(. *)$ http://oy51.com/$1 [L,R=301]
Copy the code
3. Prevent malicious domain name resolution
RewriteEngine On
RewriteCond %{HTTP_HOST} !(^www\.oy51\.com$) [NC]
RewriteRule ^(. *)$ http://www.oy51.com/$1 [L,R=301]
Copy the code
Enable Deflate
# add the following code to.htaccess to enable Deflate for the specified file suffix. Apache2.0 and later supports Deflate.
<IfModule mod_deflate.c>
<FilesMatch "\.(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
<FilesMatch "\.(js|css)$">
# enable Deflate the specified file suffix, increase the suffix can be separated by |, such as < FilesMatch "\. (js) | | | HTML CSS PHP $" >
Copy the code
5. Set the default home page and order
Add the following suffix to the.htaccess file (it can be adjusted to suit your needs) :
DirectoryIndex index.html index.htm index.php index.cgi
Copy the code
6. Do not display the directory list
# if the directory does not have a default home page file, a list of files will be listed, which may cause security risks.
To prevent this from happening, type the following command in the.htaccess file to prevent the directory list from being displayed:
Options -Indexes
Copy the code
7. Set password protection for directories and files
Create a. Htaccess file in this directory and add the following contents:
AuthUserFile /opt/guide/www.idcspy.org/.htpasswd
AuthType Basic
AuthName "Member Page"
require valid-user
AuthUserFile Sets the path to the password file.
Copy the code
8, picture anti-theft chain
RewriteCond %{HTTP_REFERER} ! ^$
RewriteCond %{HTTP_REFERER} ! ^http://(www\.) ? oy51\.com/ [NC]
RewriteRule \.(gif|jpg|png)$ - [F# because is based onHTTP_REFERER authentication, so can only prevent general image theft, because HTTP_REFERER is relatively easy to forge.
Copy the code
9, custom 404 error page
If the user enters a URL that does not exist, a custom error page is displayed
ErrorDocument 404 /404.html
# Other same
ErrorDocument 500 /500.html
Copy the code