如何将.htaccess重定向用于部分路径? [英] How can I use an .htaccess redirect for a partial path?
问题描述
我不得不调整网站上的某些路径,并且需要使用.htaccess重定向用户可能会访问旧url的项目。
I had to adjust some paths on my site and I need to use .htaccess to redirect the items on the chance a user accesses the old url.
例如我的旧网址(相对)可能是:
For example my old urls (relative) could be:
/old-path/page1.php
/old-path/page2.php
/old-path/page3.php
etc...
我不得不将路径(对于本示例而言)更改为new-path,并且我需要调整.htaccess,以便任何使用... / old-path / ...进入任何页面的人都将重定向至
I had to change the path (for this example) to new-path and I need to adjust the .htaccess so anyone that comes to any page with .../old-path/... will be redirected to
.../new-path/...
还可以满足301还是列出每个页面?
Also, would this satisfy the 301 or would I need to list out each page?
推荐答案
您可以使用mod + alias:
You can use either mod+alias:
Redirect 301 /old-path /new-path
或使用mod_rewrite:
or using mod_rewrite:
RewriteEngine On
RewriteRule ^/?old-path/(.*)$ /new-path/$1 [L,R=301]
这些可能是在文档根目录或服务器/ vhost配置中的htaccess文件中。如果您已经在某处有重写规则,则可能只想坚持使用mod_rewrite,因为在使用mod_rewrite时使用mod_alias重定向有时会产生冲突的结果。
These could be in the htaccess file in your document root or in the server/vhost config. If you already have rewrite rules somewhere, you may just want to stick with mod_rewrite because redirecting with mod_alias while using mod_rewrite can sometimes give conflicting results.
这篇关于如何将.htaccess重定向用于部分路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!