试图得到

www.example.com

直接转到

www.example.com/store

我已经尝试了多个代码位,但没有工作。

我的尝试:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

我做错了什么?


当前回答

如果你想重写URL并隐藏原始URL,另一个选择:

RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]

例如,如果您键入http://www.example.com/product.php?id=4,它将透明地打开http://www.example.com/store/product.php?id=4文件,但不会向用户显示完整的url。

其他回答

我们也可以使用Redirect来实现这个目的

Redirect 301 / www.example.com/store

或用于映射的别名

Alias / /store

编辑:mod_alias只适用于httpd.conf。

参考资料

http://httpd.apache.org/docs/2.2/mod/mod_alias.html https://httpd.apache.org/docs/trunk/rewrite/avoid.html

我认为你发布的代码的主要问题是:

第一行在严格以sample.com开头的主机上匹配,因此www.sample.com不匹配。 第二行至少需要一个字符,然后是www.sample.com,它也不匹配(为什么要转义第一个w?) 所包含的规则都没有重定向到您在目标中指定的url(另外,sample被拼错为samle,但这无关紧要)。

作为参考,以下是您当前拥有的代码:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^sample.com$
RewriteRule (.*) http://www.sample.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.sample\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

你可以使用一个重写规则,使用^$来表示根目录,并将其重写到你的/store目录,如下所示:

RewriteEngine On
RewriteRule ^$ /store [L]

您只需将此代码添加到.htaccess文件中

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /folder/index.php [L]
</IfModule>

如果文件在根目录中不存在,这将尝试子目录。需要这个,因为我移动了一个基本的。html网站,期望在根级运行,并将其推到一个子目录。只有在所有文件都是平面的情况下才有效(在子目录中不可能使用.htaccess技巧)。对于css和js文件这样的链接文件很有用。

# Internal Redirect to subdir if file is found there.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /subdir/$1 [L]