Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite rules for localhost AND live environment

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.

like image 939
Fdev Avatar asked Dec 06 '25 03:12

Fdev


1 Answers

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]
like image 187
hjpotter92 Avatar answered Dec 08 '25 03:12

hjpotter92



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!