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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动