mod_rewrite - 添加中间层目录

时间:2012-09-06 10:19:55

标签: php .htaccess mod-rewrite expressionengine

目前可在以下地址获取页面:

http://www.domain.co.uk/solutions/article/page-name1
http://www.domain.co.uk/solutions/article/page-name2
http://www.domain.co.uk/solutions/article/page-name3
etc

..但我希望通过以下地址访问它们以增强搜索引擎优化:

http://www.domain.co.uk/solutions/page-name1
http://www.domain.co.uk/solutions/page-name2
http://www.domain.co.uk/solutions/page-name3

所以,我想我想在站点根目录的.htaccess文件中使用mod_rewrite在每个传入页面请求的solutions目录之后添加'article'目录。我一直在寻找一个合适的答案大约一个月,我已经尝试过学习mod_rewrite基础知识教程,但我不能让这个为我工作,所以为另一个mod_rewrite问题道歉。

这是我目前的.htaccess文件:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Removes index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # If 404s, "No Input File" or every URL returns the same thing
    # make it /index.php?/$1 above (add the question mark)      
</IfModule>

2 个答案:

答案 0 :(得分:1)

我在我的网站上做了类似的事情。这是我用来删除index.php并显示缩短网址的.htaccess规则:

RewriteEngine On

# Removes index.php
RewriteCond $1 !\.(css|js|gif|jpe?g|png|ico) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1

RewriteCond %{REQUEST_URI} ^/solutions/(.*) [NC]
RewriteRule ^(.*) /solutions/article/%1 [L]

请记住,您在浏览器地址栏中看到的网址不是ExpressionEngine看到的网址 - EE将{segment_2}视为“文章”。

答案 1 :(得分:0)

修改:

试试这个:

<IfModule mod_rewrite.c>
RewriteEngine On 

RewriteCond %{REQUEST_URI} !^solutions/article/ 
RewriteRule ^solutions/(.*)$ /solutions/article/$1 [L,QSA] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png)$ [NC] 
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>