简体   繁体   English

mod_rewrite最简单的任务:重定向到.htaccess中的index.php

[英]Simplest task ever with mod_rewrite : redirection to index.php in .htaccess

I just created and uploaded a website using free hosting from byet.com. 我刚刚使用byet.com免费托管创建并上传了一个网站。 Everything works fine except that typing the mere domain name in the browser does not redirect the user to my index.php file (an ads page is displayed instead). 一切正常,只是在浏览器中键入纯域名不会将用户重定向到我的index.php文件(而是显示广告页面)。 After googling mod_rewrite and browsing a dozen different pages, I tried adding the following in my .htaccess file : 在谷歌搜索mod_rewrite并浏览了十几个不同的页面之后,我尝试在我的.htaccess文件中添加以下内容:

DirectoryIndex index.php

or 要么

RedirectMatch ^/$ /index.php

or also (from this Stackoverflow answer ) 或者(从此Stackoverflow 答案

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.php [L,R=302]

None of which changed anything, or sometimes caused my index.php page to be inaccessible, with the error message "The script does not redirect properly". 这些都没有改变任何内容,或者有时导致我的index.php页面无法访问,并显示错误消息“脚本无法正确重定向”。 Perhaps this behavior is implemented at the level of the httpd.conf file (to which I do not have access), so I will never be able to change that. 也许这种行为是在httpd.conf文件的级别上实现的(我无法访问它),所以我永远无法改变它。 I asked the support center of byte.com about it, but unfortunately the person who answered me did not seem to really understand English or my question. 我问了byte.com的支持中心有关它,但不幸的是,回答我的人似乎并不真正理解英语或我的问题。

Any suggestions appreciated. 任何建议赞赏。

If your index.php is under your tld (eg http://domain.com/index.php ) you can try this; 如果你的index.php在你的tld下(例如http://domain.com/index.php ),你可以试试这个;

RewriteEngine On
RewriteRule ^.*$ /index.php [L]

Otherwise, if your index.php is in a subfolder (eg http://domain.com/my/folder/index.php ), you will have to add the path to the RewriteRule; 否则,如果index.php在子文件夹中(例如http://domain.com/my/folder/index.php ),则必须添加RewriteRule的路径;

RewriteEngine On
RewriteRule ^.*$ /my/folder/index.php [L]

If this doesn't work, there is a slight possibility that your host has disabled the mod_rewrite module (although the redirection error message you get suggests otherwise). 如果这不起作用,主机有可能已禁用mod_rewrite模块(尽管你得到的重定向错误信息也是如此)。 Have you checked apache's error_log file for relevant messages? 您是否检查过apache的error_log文件以获取相关消息?

Try this rule in root .htaccess: 在root .htaccess中尝试此规则:

RewriteEngine on

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$ [NC]
RewriteRule ^(?!index\.php).*$ /index.php [L,NC,R=302]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM