dongxiao3694 2015-10-06 02:34
浏览 52
已采纳

URL重写不能正常使用.htaccess

I have implemented my small website on a live server, however I want to use url-rewrite for some links to make it clean. I try this on my localhost server:

http://localhost/cb/2/login.php to http://localhost/cb/2/login

I manage to make it work on my localhost server by editing the httpd.conf of apache and enable url-rewrite. Here's the rule I added to apache:

<IfModule mod_rewrite.c>  
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^/cb/2/login$ /cb/2/login.php   
</IfModule>

However, when I upload my files to a live server, obviously I do not have privilege to edit httpd.conf of that server, so I just put my rewrite rule on .htaccess file and here's the content of it:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/login$ /login.php [L,QSA,NC]

But when I try to test it, 404 not found is the response of server. I also try to implement in on my localhost server assuming I cannot edit the apache config and same error occurs, Object not found!

I have no idea what will I do next. I'm new to this url rewrite rule so any help will be much appreciated :)

  • 写回答

2条回答 默认 最新

  • dtkyayvldeaqhl7151 2015-10-06 02:51
    关注

    UPDATE

    After a lengthy discussion with the OP it seems there is a lot more going on behind the scenes.

    The end result is no matter what is placed in the .htaccess file... it doesn't function.

    OP was advised to contact the web host, but I will leave my original answer below for others on the site.


    Remove the forward slashes from your rewrite rule

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^cb/2/login$ cb/2/login.php [L,QSA,NC]
    

    You changed your rewrite rule used on localhost when you put it on a live server. Assuming your directory structure is the same you should keep your rewrite rule the same (minus the forward slashes).

    Remember in rewrite rules...

    1) The text between ^ and $ is what the visitor will input into the url bar, is what your link will be and is what the visitor will see in the url bar.

    2) The next block is the location where the actual file exists.

    Happy Coding !

    UPDATE

    If you want your visitor to go to www.examplewebsite.com/login.php and you want the url to look like www.example.com/login... your rewrite rule would be simply...

    RewriteRule    ^login$    login.php    [L,QSA,NC]    # Handle log-in
    

    Now... let's say that your login.php is inside another directory called admin...

    RewriteRule    ^login$    admin/login.php    [L,QSA,NC]    # Handle log-in
    

    These two lines mean...

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    

    If the url is not a "real" directory or the url is not a "real" file... then process the rule.

    That's why using RewriteRule ^login$ index.php causes your url to be rewritten as www.examplesite.com/login.php because the rewrite rule cannot be processed (index.php could not be found), BUT login.php is a "real" file.... so it goes to it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?