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条)

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面