CodeIgniter 中的 Mod 重写新手

发布于 2024-09-12 05:47:59 字数 1344 浏览 1 评论 0原文

我对 mod-rewrites 还很陌生,而且我很难正确地做到这一点:

我想要 http:// myurl.com/specialhttp://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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-09-19 05:47:59

哦,不,你不需要 htaccess 来做到这一点,只需使用 CI 的路由即可。首先将您的 HTACESS 更改为:

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

这个要好得多,您无需为任何非 img 文件夹或任何内容更改它,只需保持纯净即可。现在解决你的问题。

在Application->config文件夹中打开routes php。

现在,在 CI 中,所有文件默认都通过 index.php 路由,所以我假设您的意思是您的默认控制器无论如何都将“控制器”路由到您的情况,特别是回到主站点,只需执行以下操作:

如果您的默认控制器仍然是“欢迎”当它被下载时,你会写一个这样的路线:

$route['special/(:any)'] = "welcome";

你的网址不会改变,一切都会完美

但不要停止阅读,只是为了确定

这就是你的路由完成后的样子:

//these two are for CI 
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

//this is your route sending all and any traffic for special
//no matter how many levels it will always go to welcome->index.
$route['special/(:any)'] = "welcome";

//if you want to use sub-pages or other functions within your welcome controller 
//i.e. welcome->signup. then just send the route information to the controller like this:
$route['special/(:any)'] = "welcome/$1";

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:

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>

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:

$route['special/(:any)'] = "welcome";

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:

//these two are for CI 
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

//this is your route sending all and any traffic for special
//no matter how many levels it will always go to welcome->index.
$route['special/(:any)'] = "welcome";

//if you want to use sub-pages or other functions within your welcome controller 
//i.e. welcome->signup. then just send the route information to the controller like this:
$route['special/(:any)'] = "welcome/$1";

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

把回忆走一遍 2024-09-19 05:47:59

假设 www.myurl.com 和 myurl.com 都在您的 VirutalHost 中设置为 ServerAliases,那么就像...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Assuming www.myurl.com and myurl.com are both set-up as ServerAliases in your VirutalHost, then something as simple as...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文