CodeIgniter 中的 Mod 重写新手
我对 mod-rewrites 还很陌生,而且我很难正确地做到这一点:
我想要 http:// myurl.com/special 和 http://www.myurl.com/special 到两者都重定向到 http://myurl/index.php/special 而不更改访问者的 url他们的浏览器。
目前,我的 .htaccess 看起来像这样,为了从 URL 中删除 www,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [L,R=301]
</IfModule>
我尝试了很多不同的方法来重定向特殊的 URL,但到目前为止,没有任何方法对我有用,每种方法都有不同的不良结果。如果您对我可以在此处添加的内容有任何想法,请告诉我!
我尝试了这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^special$ /index.php/special$1 [NC]
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [R=301]
</IfModule>
这使得 http://myurl.com/special 重定向到默认控制器,并使 < a href="http://www.myurl.com/special" rel="nofollow noreferrer">http://www.myurl.com/special 重定向到 http://myurl.com/index.php/special 并更改浏览器中的网址。尽管第二个更接近我的需要,但这两个都不完全正确。
I'm pretty new to mod-rewrites, and I'm having trouble getting this right:
I'd like http://myurl.com/special and http://www.myurl.com/special to both redirect to http://myurl/index.php/special without changing the url for the visitor in their browser.
currently, my .htaccess looks like this, to remove www from URLs
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [L,R=301]
</IfModule>
I tried a bunch of different things to redirect the special URL, but nothing has worked for me so far, each with a different undesirable result. Please let me know if you have an idea about what I can add here!
I tried this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^special$ /index.php/special$1 [NC]
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [R=301]
</IfModule>
That makes http://myurl.com/special redirect to the default controller, and makes http://www.myurl.com/special redirect to http://myurl.com/index.php/special AND changes the url in the browser. Neither of those is quite right, although the second is closer to what I need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,不,你不需要 htaccess 来做到这一点,只需使用 CI 的路由即可。首先将您的 HTACESS 更改为:
这个要好得多,您无需为任何非 img 文件夹或任何内容更改它,只需保持纯净即可。现在解决你的问题。
在Application->config文件夹中打开routes php。
现在,在 CI 中,所有文件默认都通过 index.php 路由,所以我假设您的意思是您的默认控制器无论如何都将“控制器”路由到您的情况,特别是回到主站点,只需执行以下操作:
如果您的默认控制器仍然是“欢迎”当它被下载时,你会写一个这样的路线:
你的网址不会改变,一切都会完美
但不要停止阅读,只是为了确定。
这就是你的路由完成后的样子:
php 中的路由类非常强大,这只是这里的一部分,有关路由的更多详细信息,请参阅此页面:
CodeIgniter 用户指南 -> URI 路由
真的希望我没有让你更加困惑。祝你好运
oh no you don't need htaccess to do that just use CI's routes. Here first change your HTACESS to this:
This one is way way better and you never have to change it for anything not for img folders or anything just leave it pure. now for your issue.
open routes php in the Application->config folder.
Now in CI all files route through index.php by default so i am assuming you mean your default controller anyway to route "controllers" like in your case special back to the main site just do this:
if your default controller is still "welcome" as it is downloaded you would write a route like this:
your url will not change and everything will be perfect
BUT DONT STOP READING YET, JUST TO MAKE SURE.
this is what your routes would look like after your done:
The routing class in php is very powerful and this is just a piece here for more detailed info on routing see this page:
CodeIgniter User Guide -> URI Routing
Really hope i haven't confused you more. Good Luck
假设 www.myurl.com 和 myurl.com 都在您的 VirutalHost 中设置为 ServerAliases,那么就像...
Assuming www.myurl.com and myurl.com are both set-up as ServerAliases in your VirutalHost, then something as simple as...