Apache server USES.htaccess to implement pseudo static URL methods

  • 2020-05-14 05:23:54
  • OfStack

Many friends may be space is virtual host, they do not have the authority to manage apache httpd.conf file, natural is not likely to be able to write static rules on this, we can let the host business open support.htaccess method, so you can write.

First, configure the server to start the rewrite module
Open the Apache configuration file httpd.conf.
Remove the # before #LoadModule rewrite_module modules/mod_rewrite.
Restart Apache after saving


Write rewrite rule
For example, the code in test.php is


<?php
$id=$_GET["id"];
echo $id;
?> 

The function is to accept the id parameter in URL and display it on the page.
Start with notepad (or editplus if that doesn't work), create a new document, save it as.htaccess, and write the following code into it


.<IFMODULE mod_rewrite.c>
.RewriteEngine On
.RewriteBase /
.RewriteRule ^t_(.*).html$ test.php?id=$1 [L]
.</IFMODULE> 

See the apache manual for an explanation. Download the apache 2.2 manual
Line 4 ^t_(.*).html$describes the URL address you entered, test.php ? id=$1 the actual address to be accessed
For example, when you type test.php and.htaccess in your browser (assuming your test.php and.htaccess files are in your server's mytest folder), localhost/mytest/ t_1.html will print 1 in your browser.
If the input is localhost/mytest/ t_sophp.html, the browser outputs sophp
First address the actual access is localhost/mytest/test php & # 63; id = 1
Second is actually access localhost/mytest/test php & # 63; id = sophp

After rewriting the rules, change the URL link in your web page to your modified rule style.
If the space you buy supports overwriting, simply transfer the.htaccess file to the root directory.


Related articles: