我的 public_html 文件夹的 b1 文件夹中有一个 codeigniter 项目:
mydomain.com/b1/controller/function
我想将其更改为:
mydomain.com/controller/function
在浏览器栏中使用 301 重定向。
我的 public_html 中的 .htaccess 文件有:
RewriteEngine on
RewriteRule ^b1/(.*)$ /$1 [R=301,L,NC]
RewriteRule ^(.*)$ b1/$1
EXPLANATION:
RewriteRule ^b1/(.*)$ /$1 [R=301,L,NC] # THIS IS TO REWRITE ALL REQUESTS FROM THE NAVBAR IN MY PROJECT WHICH HAVE THE FORM mydomain.com/b1/controller/function TO mydomain.com/controller/function. AFTER THIS THE NAVBAR SHOULD SHOW mydomain.com/controller/function ( I ASSUME )
RewriteRule ^(.*)$ b1/$1 # THIS IS TO TAKE ALL INCOMING REQUESTS AND ADD A b1 IN FRONT SO mydomain.com/controller/function IS REDIRECTED TO mydomain.com/b1/controller/function - BUT INTERNALLY, SO THAT THE BROWSERS ADDRESS BAR STILL SHOWS A 'PRETTIER' mydomain.com/controller/function
根据http://htaccess.madewithlove.be/,这应该可行,但我仍然看到:
mydomain.com/b1/controller/function
在我的地址栏中。
请注意,这是一个 codeigniter 项目,CI 有以下 .htaccess 文件。(该项目再次位于我的 public_html/b1 目录中)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
据我所知, mod_rewrite 正在工作,因为 RewriteRule ^(.*)$ b1/$1 在与上面的规则不匹配时工作。
2 .htaccess 文件的某些组合是否会导致地址栏中缺少可见的重写?我想对此进行调试并弄清楚如何实现上面讨论的 URL 格式。
谢谢,
账单
附录:感谢大家的尝试。我注意到多个答案是:
RewriteRule ^b1/(.*)$ /$1 [R=301,L,NC]
RewriteRule ^(.*)$ b1/$1 [L]
他们似乎有很多L标志。在伪代码中,我想要实现的是:
IF REQUEST is mydomain.com or www.mydomain.com ( with nothing after the /) rewrite to mydomain.com . FIRST REWRITE TO REMOVE B1 IF EXISTS ( this is for the navbar requests which have form: mydomain.com/b1/controller/function which should be rewritten to mydomain.com/controller/function ) so that the address bar shows mydomain.com/controller/function
如果上面有一个 L ,我认为它不会起作用
Then add b1 back in internally so you redirect to b1 folder where project is stored