Apache .HTACCESS技巧-重写动态内容

Apache .HTACCESS技巧-重写动态内容,apache,mod-rewrite,Apache,Mod Rewrite,我需要能够做到这样,当有人访问我的网站时,可以说: a) 它实际上显示了以下内容: b) 如果用户在(b)中输入,那么他们将被301'd改为(a) 在我的HT访问中,我目前拥有以下内容: <IfModule mod_rewrite.c> RewriteEngine on # index.php to / RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP RewriteRule (.*)index\.(php|

我需要能够做到这样,当有人访问我的网站时,可以说:

a)

它实际上显示了以下内容:

b)

如果用户在(b)中输入,那么他们将被301'd改为(a)

在我的HT访问中,我目前拥有以下内容:

<IfModule mod_rewrite.c>
RewriteEngine on


# index.php to /
RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
RewriteRule (.*)index\.(php|html)$ /$1 [R=301,L]

# force www.
rewritecond %{HTTP_HOST} ^mysite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]

RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.mysite.com%{REQUEST_URI}? [R=301,L]

</IfModule>

重新启动发动机
#index.php到/
RewriteCond%{THE_REQUEST}^GET\/.*/index\.(php | html)\HTTP
重写规则(.*)索引\.(php | html)$/$1[R=301,L]
#强制www。
重写cond%{HTTP_HOST}^mysite.com[nc]
重写规则^(.*)$http://www.mysite.com/$1[r=301,北卡罗来纳州]
RewriteCond%{THE_REQUEST}^GET\/.*\.*.\HTTP/
重写cond%{QUERY_STRING}^$
重写规则。*http://www.mysite.com%{REQUEST_URI}?[R=301,L]

我不确定您所描述的内容是否可以只使用重写规则。但您可以结合使用重写规则和PHP,如下所示:

将.htaccess文件更改为如下所示(注意:假设.htaccess文件位于根目录下)


重新启动发动机
#强制www。
重写cond%{HTTP_HOST}^www\.mysite\.com$[NC]
重写规则^(.*)$http://www.mysite.com/$1[R=301,北卡罗来纳州]
重写cond%{REQUEST_URI}^/文章/索引(-new)?\.php
#这是寻找任何过去的文章,不是一个/。你可以
#需要将其更改为更具包容性-类似于(.*)的内容
重写规则^article/([^\/]+)/article/index new.php?src=$1[L,NC]
#index.php到/
重写规则^index\(php | html)$/[R=301,L]
#RewriteCond%{THE_REQUEST}^GET\/.*/index\.(php | html)\HTTP
#重写规则(.*)索引\.(php | html)$/$1[R=301,L]
RewriteCond%{THE_REQUEST}^GET\/.*\.*.\HTTP/
重写cond%{QUERY_STRING}^$
重写规则。*http://www.mysite.com%{REQUEST_URI}?[R=301,L]
您的文章/index.php将成为指向新index.php的重定向器,其外观如下:

<?php
    if (isset($_GET["src"]))
        header("location: /article/" . basename($_GET["src"])); // Using basename() in case src happens to be referring to an actual file in your code
    else
        header("location: /");
?>

您的“真实”索引实际上是index-new.php,这是重写规则将调用的

<?php
    if (isset($_GET["src"]))
        header("location: /article/" . basename($_GET["src"])); // Using basename() in case src happens to be referring to an actual file in your code
    else
        header("location: /");
?>