mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面。下面我详细说说它的使用方法
1.检测Apache是否支持mod_rewrite
通过php提供的phpinfo()函数查看环境配置,通过Ctrl+F查找到“Loaded Modules”,其中列出了所有apache2handler已经开启的模块,如果里面包括“mod_rewrite”,则已经支持,不再需要继续设置。
如果没有开启“mod_rewrite”,则打开目录 您的apache安装目录“/apache/conf/” 下的 httpd.conf 文件,通过Ctrl+F查找到“LoadModule rewrite_module”,将前面的”#”号删除即可。
如果没有查找到,则到“LoadModule” 区域,在最后一行加入“LoadModule rewrite_module ,modules/mod_rewrite.so”(必选独占一行),然后重启apache服务器即可。
2.让apache服务器支持.htaccess
如何让自己的本地APACHE服务器支持”.htaccess”呢?其实只要简单修改一下apache的httpd.conf设置就可以让 APACHE支 持.htaccess了。打开httpd.conf文件(在那里? APACHE目录的CONF目录里面),
用文本编辑器打开后,查找
代码如下:
Options FollowSymLinks
AllowOverride None
改为
代码如下:
Options FollowSymLinks
AllowOverride All
就可以了。
3.建立.htaccess 文件
有1种最简单的方法建立.htaccess文件:
用记事本 打开,点击文件–另存为,在文件名窗口输入”.htaccess”,注意是整个绿色部分,
也就是包含英文引号,然后点击保存就行了。
4.rewrite规则学习
我们新建一个.htaccess文件之后,就在里面写入以下内容:
RewriteEngine on #rewriteengine为重写引擎开关on为开启off为关闭
RewriteRule ([0-9]{1,})index.php?id=1
我讲解一下RewriteRule:RewriteRule是重写规则,支持正则表达式的,上面的([0-9]{1,})是指由数
字组成的,是结束标志,说明是以数字结束!好吧,现在我们可以实现伪静态页面了,写下一下的规则:RewriteEngineonRewriteRule([a−zA−Z]1,)−([0−9]1,).htmlindex.php?action=1&id=2
([a-zA-Z]{1,})-([0-9]{1,}).html是规则,index.php?action=1&id=2是要替换的格式,1代表第
一个括号匹配的值,2代表第二个,如此类推!!我们写一个处理的PHP脚本:index.phpPHP代码echo‘你的Action是:′._GET['action'];
echo ‘
';
echo ‘你的ID是:' . $_GET['id'];
?>
好了,我们现在在浏览器中输入:
localhost/page-18.html
输出的是:
你的Action是:page
你的ID是:18
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
查看apache的httpd.conf没有LoadModule rewrite_module modules/mod_rewrite.so这一行,则需要手动生成此模块,方法如下:
apache源码包路径为:/ftp/httpd2.2
apache安装路径:/usr/local/apache/
一、增加mod_rewrite模块
代码如下:
# find . -name mod_rewrite.c //在apache的源码安装目录中寻找mod_rewrite.c文件
./modules/mappers/mod_rewrite.c
# cd /ftp/httpd2.2/modules/mappers //进入包含mod_rewrite.c文件的目录
# /usr/local/apache/bin/apxs -c mod_rewrite.c //apxs应指定绝对路径,在你当前正在使用apache的bin目录里
# /usr/local/apache/bin/apxs -i -a -n mod_rewrite mod_rewrite.la
如果没有什么错误的话,应该在你的apache的modules目录(/usr/local/apache/modules)中编译出一个mod_rewrite.so文件。
编辑httpd.conf文件,确认httpd.conf中已经包含mod_rewrite.so的加载语句,如下:
LoadModule mod_rewrite_module modules/mod_rewrite.so
二、重启apache
1、如果出现以下错误:
httpd: Syntax error on line 54 of /usr/local/apache/conf/httpd.conf: Can't locate API module structure `mod_rewrite_module' in file /usr/local/apache/modules/mod_rewrite.so: /usr/local/apache/modules/mod_rewrite.so: undefined symbol: mod_rewrite_module
需要把
LoadModule mod_rewrite_module modules/mod_rewrite.so
改为
LoadModule rewrite_module modules/mod_rewrite.so
2、如果出现以下错误
httpd: Syntax error on line 54 of /usr/local/apache/conf/httpd.conf: module rewrite_module is built-in and can't be loaded
表示模块是内建的,不用再调入,注释掉
#LoadModule rewrite_module modules/mod_rewrite.so
三、对某一虚拟站点开启url重写
1、编辑httpd-vhost.conf,在对应的站点标签内加入:
代码如下:
Directory /website/webA
AllowOverride All
/Directory
即:
代码如下:
VirtualHost *:80
DocumentRoot /website/webA
ErrorLog logs/webA.com-error_log
CustomLog logs/webA.com-access_log common
Directory /website/webA
AllowOverride All
/Directory
/VirtualHost
四、把对站点的重写规则保存到站点根目录下,文件名为“.htaccess”
五、重启apache即可

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
