banner
阿珏酱

阿珏酱

乘上与平常相反的电车,去看看那未曾见过的风景
twitter
github
facebook
bilibili
zhihu
steam_profiles
youtube

.htaccess pseudo-static rules

Tips: When you see this prompt, it means that the current article has been migrated from the original emlog blog system. The publication time of the article is too long ago, and the formatting and content may not be complete. Please understand.

.htaccess pseudo-static rules

Date: 2017-12-4 Ajue Code Fiddling Views: 1701 Comments: 1

image Apache's mod_rewrite is quite powerful and can be used to achieve pseudo-static functionality when building websites.

Checking if Apache has enabled mod_rewrite#

You can use the phpinfo() function provided by PHP to view the environment configuration. Find "Loaded Modules" and it lists all the modules that apache2handler has enabled. If "mod_rewrite" is included, it means that it is already supported and there is no need to continue setting up.

If "mod_rewrite" is not enabled, open the "httpd.conf" file in the "apache/conf/" directory, and find "LoadModule rewrite_module". Remove the "#" in front of it to enable this functionality.

If you cannot find the "LoadModule" section, you can add "LoadModule rewrite_module ,modules/mod_rewrite.so" (on a separate line) at the end of the file, and then restart the Apache server.
After that, use the phpinfo() function to check the environment configuration, and you should see "mod_rewrite" as an item.

Making Apache server support .htaccess#

How can you make your local APACHE server support ".htaccess"? You only need to modify the httpd.conf settings of Apache. Open the httpd.conf file in the CONF directory of the APACHE directory, and find: Options FollowSymLinks AllowOverride None Change it to Options FollowSymLinks AllowOverride All.

Creating the .htaccess file#

When creating the .htaccess file, please note that you cannot create it directly. The method is to use the "Save As" menu in Notepad, enter ".htaccess" in the file name window, and then click Save.

Learning rewrite rules#

RewriteEngine on #rewriteengine is the switch for the rewrite engine, "on" means enable and "off" means disable.

RewriteRule ([0-9]{1,})$index.php?id=$1 Here, RewriteRule is the rewrite rule, which is a sentence using regular expressions. ([0-9]{1,}) represents a number, and $ represents the end flag, indicating that it ends with a number!

If you want to implement pseudo-static pages, the rule is as follows:

RewriteEngine on RewriteRule ([a-zA-Z]{1,})-([0-9]{1,}).html$index.php?action=$1&id=$2

Here is the .htaccess file for my blog#

<IfModule mod_rewrite.c>
RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^post-([0-9]{1,}).html$ index.php?post=$1 
</IfModule>

Rule for forcing http to redirect to https#

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]

User Comments:

image Guangdong Self-study Network 6 months ago (2020-08-28)
I learned a lot. Thanks for the blogger's sharing.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.