Simple Mod Rewrite

前端 未结 3 1807
盖世英雄少女心
盖世英雄少女心 2020-12-11 06:14

I\'m trying to create a mod rewrite to simply turn these links:

/index.php?page=home
/?page=home

into

/home
相关标签:
3条回答
  • 2020-12-11 06:43
    RewriteRule .*\?page=(\w+)$ /$1/ [NC]
    

    Should do the trick but maybe you could be more specific in which cases need to work?

    Heres a website you can read up on htaccess

    http://corz.org/serv/tricks/htaccess2.php

    0 讨论(0)
  • 2020-12-11 06:57

    There are a variety of ways to do this. This one applies the rule to any non-existent file:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?page=$1 [L]
    

    Edit: You may have to add RewriteEngine on at the top, and this goes in .htaccess in your web document root. All of this, of course, depends on whether use of htaccess (mod_rewrite) is allowed by your web host and whether the rule override file should be named .htaccess. Both are pretty common though, so it shouldn't be a problem.

    0 讨论(0)
  • 2020-12-11 06:58

    If I understand, you want your end users to enter example.com/home and have that rewritten in to /index.php?home?

    RewriteEngine On
    # /home or / get redirected...
    RewriteRule ^(home)?$ /index.php?page=home [L,QSA]
    

    To debug, you can enable the RewriteLog. However, as suggested in the comments it should be used for debugging only. It is best to disable it or set the level very low in production.

    RewriteLog "/path/to/logs/rewrite.log"
    # Increase the log level (default 0, >4 gets pretty verbose) 
    RewriteLogLevel 3 
    
    0 讨论(0)
提交回复
热议问题