Rewrite rules for localhost AND live environment

三世轮回 提交于 2019-12-07 19:34:43

问题


I want to add rewriterules that works in local environment (localhost) and on my liveserver.

Why?

I don't want to change rules when I test my project locally and upload it to the liveserver.

  1. Adding Rules

    Here an example

    (ANY-URL)index.php?page=somepage
    

    change to

    (ANY-URL)/somepage
    

    So I used a rewritemod generator and pasted this into it:

    index.php?page=somepage
    

    The rewriterule I got, looks like this: (of course my .htacces starts with RewriteEngine On)

    RewriteRule ^([^/]*)$ /?page=$1 [L]
    

    When I try to get to (http:) //localhost/myproject/development/index.php?page=login it sends me to the root directory of my local development envirment. But the URL in the adressline doesn't change.

  2. Testing

    Of course I tried some other Rules by pasting the whole URL into the generator just to test if the rewrite thing works. Also here the URL doesn't change to short-url but the server cant find stylesheets and javascripts anymore. I got redirected to the index.php

  3. Possible solutions?

    • Maybe it has something todo with that "RewriteBase"?
    • Do i have to set a basepath?

My .htacces is located here:

//localhost/myproject/development/.htaccess

Later I also want to change paths that look like this:

(ANY-URL)index.php?page=somepage&second=hello&third=world&fourth=cool

Also here a I'm looking for a solution that works on both environments.


回答1:


Since the htaccess is located inside a sub-directory, use RewriteBase:

RewriteEngine On
RewriteBase /myproject/development/

Once that is done, you use the base element in your php/html files. This will resolve the addresses for the scripts/stylesheets in your pages.

<base href="/myproject/development/" />

Now, the rewrites in htaccess would be:

RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]


来源:https://stackoverflow.com/questions/33045018/rewrite-rules-for-localhost-and-live-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!