doubeishuai6598
2012-11-18 20:24
浏览 79
已采纳

网址重写 - 始终重定向到index.php

i'm trying to rewrite some simple urls:

http://www.site.com/folder/index.php?page=something

into

http://www.site.com/folder/something

here's my .htaccess file ( placed in /folder/ )

RewriteEngine On
RewriteBase /folder
RewriteRule ^(.*)$ index.php?page=$1 [R]

with this rule i always get this URL

    http://www.site.com/folder/index.php?page=index.php

what am i doing wrong?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试重写一些简单的网址:

   http://www.site.com/folder/index.php?page=something

into

 http://www.site.com/folder/something
   
 
 

这是我的.htaccess文件(放在/ folder /中) \ n

  RewriteEngine On 
RewriteBase / folder 
RewriteRule ^(。*)$ index.php?page = $ 1 [R] 
   
 
 

规则我总是得到这个URL

  http://www.site.com/folder/index.php?page=index.php

我做错了什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • doushou6480 2012-11-18 22:44
    已采纳

    First, change

     
    RewriteBase /folder
     
    

    to

    
    RewriteBase /folder/ 
    
    

    Second, change

     
     RewriteRule ^(.*)$ index.php?page=$1 [R]
     
    

    to

     
      RewriteRule ^index\.php/(.*)$ index.php?page=$1 [L,QSA]
     
    

    That should work. The QSA part is to allow any querystring in the address to be carried over to the new address; otherwise, they will be dropped.

    Edit

    In that case, try this

     
      RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
     
    

    similar to what you had earlier.
    Unless you have other patterns, that should achieve what you need

    Hope it helps.

    点赞 打赏 评论
  • dongqiao6445 2012-11-18 20:29

    You are referencing the wrong match, use $2 like so

    RewriteEngine On

    RewriteBase /folder

    RewriteRule ^(.*)$ index.php?page=$2 [R]

    The above is infact incorrect. It seems that the . is not matching the correct characters (I don't know why).

    The best way I can see getting round this without knowing the cause of the issue is to simply match letters and numbers like so:

    # Turn on the rewrite engine
    RewriteEngine On
    
    # Set the rewrites for /folder
    RewriteBase /folder
    
    # Set the rewrite rules
    RewriteRule ^([a-z0-9]+)/?$ index.php?page=$1 [NC]
    
    点赞 打赏 评论
  • drob50257447 2012-11-18 20:30

    If I may suggest, please look into a generator for htaccess, such as: http://www.generateit.net/mod-rewrite/

    which gives you:

    RewriteEngine On
    RewriteRule ^folder/([^/]*)$ /folder/index.php?page=$1 [L]
    

    I've editted the above, this is a working example on a local testdomain. Perhaps this is just what you have been looking for.

    Also, if I may, omitting the extension of a page is not recommendable. Google likes it to be recognizable e.g. .html or .php.

    And, you should add a rewriterule for every parameter you add.

    Finally, I would omit the index.php. Your server will automatically load index.php if no other page is found, but it doesn't have to be visible in the addressbar.

    点赞 打赏 评论
  • dongmufen8105 2012-11-18 23:40

    Your initial rule worked perfectly. The only problem was the [R] modifier which redirected /folder/something to /folder/index.php?page=something and caused a second request.

    Apply your rule on /folder/index.php?page=something and you get /folder/index.php?page=index.php

    To fix this replace [R] ( HTTP 302 Redirect ) by [L] ( leave matching chain when you match here ) and you're done

    点赞 打赏 评论
  • dpv21589 2012-11-19 11:02

    Thanks to everyone i succeeded in doing what i was thinking to do:

    RewriteEngine On
    RewriteBase /folder/
    
    #Excluding direct url to php files
    RewriteCond %{REQUEST_URI} !(.*)\.php$
    #Excluding the empty space url
    RewriteCond %{REQUEST_URI} !^(\s*)$
    RewriteCond %{REQUEST_URI} !\.(gif|jpg|png|css|js|)$ [NC]
    RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
    
    点赞 打赏 评论

相关推荐 更多相似问题