简体   繁体   English

mod_rewrite和301重定向在同一htaccess中

[英]mod_rewrite and 301 redirect in the same htaccess

I have the following .htaccess file: 我有以下.htaccess文件:

RewriteEngine on
Options +FollowSymLinks

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule (.*)$ ./page.php?name=$1

I also have about 20 different static addressess I want to do a 301 redirect to. 我还想将301重定向到20个左右的静态地址。

It should look something like: 它看起来应该像这样:

Redirect 301 http://www.example.com/category.php?id=3 http://www.example.com/books
Redirect 301 http://www.example.com/articles.php?id=124 http://www.example.com/birds

I tried every method and failed. 我尝试了每种方法都失败了。

Can somebody help me? 有人可以帮我吗?

I'm guessing your after a set of SEO friendly URL's for those in your redirect examples, if this is the case then you don't want a browser redirect, instead you want an Internal Redirect or Alias eg 我猜你为您的重定向示例中的那些设置了一系列SEO友好URL ,如果是这种情况,则您不希望浏览器重定向,而是希望使用内部重定向或别名,例如

RewriteRule ^/?books /category.php?id=3    [L]
RewriteRule ^/?birds /articles.php?id=124  [L]

If you have many rules and have access to the httpd.conf you may want to consider a rewrite map file and rule. 如果您有很多规则并且可以访问httpd.conf ,则可能需要考虑重写映射文件和规则。

Per your comment BELOW, if you want to force users/crawlers to the new URL structure then you'll need an additional set of Rewrite rules in the file (not redirects) eg 根据下面的评论,如果要强制用户/爬网程序使用新的URL结构,则文件中将需要一组其他的重写规则(而非重定向),例如

RewriteCond %{THE_REQUEST}      /category\.php     [NC]
RewriteCond %{QUERY_STRING}     ^id=3  [NC]
RewriteRule ^.*                 http://%{HTTP_HOST}/books?   [R=301,L]

FYI: The Apache mod_rewrite documentation is worth a read if your unclear as to what the above rules do, or if you want something a little more abstract consider the following post . 仅供参考:如果您不清楚上述规则的用途,或者如果您想要更抽象的内容,请阅读以下文章 ,以阅读Apache mod_rewrite文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM