Joomla默认情况下打开链接,url中都会出现index.php,看起来挺别扭的,琢磨着将其去掉,网上有提供很多方法,同样的方法,在我机器上却不能成功,经过好两个小时的摸索,总算找到了解决方案,做一个简单的整理,希望能对初学者有帮助。
网上一些文章提供了解决方法,但在我的机器上一直没有成功,推测是apache版本的问题,这里晒下我的配置信息。
Apache Version:Apache/2.2.14 (Win32) PHP/5.3.10
Joomla Version : 2.5.19
localhost根目录:H:\www
Joomla网站目录:H:\www\joomla_test
1. 开启Joomla的SEF模式
在后台管理全局设置中开启如下两个选项:
(1) 搜索引擎友好链接,即SEF
(2) 使用Apache重写模式
如下图:
2. 创建.htaccess文件
将H:\www\joomla_test下的htaccess.txt文件另存为.htaccess(注意,windows下不能直接重命名,我使用Notpad++另存为来实现)
3. 配置apache的httpd.conf文件
在apache安装目录下conf目录中的httpd.conf文件中开启mod_rewrite模块,具体修改方法:
找到LoadModule rewrite_module modules/mod_rewrite.so行,将前面的#号去掉即可。
最后重启apache。
上面两步是网上大多数文章提到了,我照着做完,刷新页面,index.php倒是去掉了,但页面出现404 Not Found错误。
直到我google到了这个网站:
Enabling SEF causes "Page not found" error:
http://forum.joomla.org/viewtopic.php?t=242684
原文如下:
FIRST - Test if mod_rewrite is working Having problems getting Joomla core SEF to work? If mod_rewrite is not working, nothing else you do matters. Test this first. Test if mod_rewrite is working Put only these two lines in your .htaccess file. Code: Options +FollowSymLinks Redirect /google.html http://www.google.com Now point your browser to: http://www.yoursite.com/google.html If it redirects you to Google - mod_rewrite is working. If it gives you an error - mod_rewrite is not working. Note: Replace "www.yoursite.com" with your actual web site domain. ----- If mod_rewrite is not working, check Apache configuration. Apache requirements for mod_rewrite to work The Apache httpd.conf file has the settings to enable URL rewriting. Two settings are required. 1. mod_rewrite module is installed Search file for LoadModule rewrite_module and un-comment that line (remove the leading #) 2. AllowOverride All Search file for AllowOverride None and change it to AllowOverride All It occurs in 2-3 different places; change all. 3. Restart Apache Restarting the Apache web server is required for the changes to take effect. SECOND - OpenSEF Simplified .htaccess File After you have confirmed that mod_rewrite is working Try this as your htaccess file:
Code:
## OpenSEF Simplified htaccess File ## Can be commented out if causes errors Options +FollowSymLinks ## enable mod_rewrite engine RewriteEngine On ## If Joomla is installed in the web server root ## RewriteBase / ## If Joomla is installed in a sub-directory ## RewriteBase /directory_name_here RewriteBase / ## Begin 3rd-Party or OpenSEF Section #RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR] ##optional - see notes## RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*) index.php ## End 3rd-Party or OpenSEF Section ## Joomla Security Section (has nothing to do with SEF/SEO) ## Begin - Rewrite rules to block out some common exploits ## If you experience problems on your site block out the operations listed below ## This attempts to block the most common type of exploit `attempts` to Joomla! # Block out any script trying to set a mosConfig value through the URL RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR] # Block out any script trying to base64_encode crap to send via URL RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR] # Block out any script that includes a tag in URL RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR] # Block out any script trying to set a PHP GLOBALS variable via URL RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR] # Block out any script trying to modify a _REQUEST variable via URL RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) # Send all blocked request to homepage with 403 Forbidden error! RewriteRule ^(.*)$ index.php [F,L] ## End - Rewrite rules to block out some common exploits This is simply the J 1.0.12 htaccess file with the unneeded comment text removed and the standard Joomla core SEF rewrite section removed. What is left is just the lines of the file actually needed. |
作者提到了一个很关键的问题:
FIRST - Test if mod_rewrite is working
也就是测试mod_rewrite是否成功开启?
参考链接已经给出了很多测试方法,我自己测试后的结果是,mod_rewrite并没有开启!(测试的时候记得将.htaccess文件放在localhost根目录中,我的是H:\www,而不是H:\www\joomla_test中,切记!)
原因如作者所料,AllowOverride None导致我根本就没有启用.htaccess文件,于是继续配置httpd.conf文件
4. 在httpd.conf文件中配置启用htaccess
将httpd.conf中搜索AllowOverride,将
AllowOverride None
全部改成:
AllowOverride All
(其实不需要全部改,目前只需要网站根目录下的Directory中的AllowOverride,如我的只需要改<Directory "H:/www">中的AllowOverride,当然全部替换也是错的)
重启apache服务器,刷新页面,
问题依旧。
在这里不得不提到本地joomla网站的特殊性,我本机host的根目录是
H:/www,而我的网站是其中的一个子目录及joomla_test/,.htaccess也位于joomla_test目录下,于是还需要修改.htaacess中RewriteBase字段
5. 修改.htaacess中RewriteBase字段
默认是不需要修改的,但考虑网站位于根目录下的一个子目录中,需要将其修改为:
RewriteBase /Joomla_test
刷新页面,index.php被去掉,页面也能正常加载了。
说道htaccess文件,再附上一个链接,当是普及了