试图得到

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]

我做错了什么?


当前回答

这是我用来重定向到子目录的。这是无形的,但仍然允许通过匹配现有文件或其他文件的请求。

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdir/index.php [L]

用你的值替换site.com和subdir。

其他回答

试试这个:

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

如果你想要一个外部重定向(这导致访问浏览器显示重定向URL),设置R标志也在那里:

RewriteRule ^$ /store [L,R=301]

我们也可以使用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

我很惊讶居然没有人提到这一点:

RedirectMatch ^/$ /store/

基本上,它只重定向根URL。 答案就来自这个链接

如果你想重写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。

尝试在htaccess中使用下面的行

注意:你可能需要检查default.html的名称是什么

html是根文件夹中默认加载的文件。

RewriteEngine

重定向/default.html http://example.com/store/