【问题标题】:how to remove folder name from url using htaccess如何使用 htaccess 从 url 中删除文件夹名称
【发布时间】:2013-09-29 03:56:44
【问题描述】:

我想更改网址:

http://example.com/Portfolios/iPhone/app

收件人:

http://example.com/iPhone/app

所有网址都一样:

example.com/Portfolios/iPad/app

收件人:

example.com/iPad/app

来自:

example.com/Portfolios/xyz/app

收件人:

example.com/xyz/app

我尝试了很多,但没有什么对我有用。


<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^Portfolios(/.*|)$ $1 [L,NC]  
</IfModule>

【问题讨论】:

标签: regex apache .htaccess mod-rewrite redirect


【解决方案1】:

启用mod_rewrite.htaccesshttpd.conf,然后将此代码放入您的.htaccess DOCUMENT_ROOT 目录下

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^Portfolios/(.*)$ /$1 [L,NC,R]

说明: 上面的规则匹配以Portfolios 开头并具有/Portfolios/xyz/app 之类的内容并将xyz/app 放入$1 的URL 模式。它将外部重定向到/$1,即/xyz/app

这些是使用的标志:

L  - Last
NC - Ignore (No) Case comparison
R  - External redirection (with 302) -- can be changed to R=301

【讨论】:

【解决方案2】:

您还可以将根目录设置为 /var/www/Portfolios 而不是 /etc/apache2/sites-enabled 中的 /var/www/,方法是将 DocumentRoot 行编写为

DocumentRoot /var/www/Portfolios

而不是 文档根目录 /var/www/ 还有这条线 改为

【讨论】:

    猜你喜欢
    • 2016-04-13
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 2020-03-19
    • 2020-02-08
    • 2014-04-22
    • 2015-06-23
    相关资源
    最近更新 更多