3

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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Fdev
  • 63
  • 1
  • 6

1 Answers1

3

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]
hjpotter92
  • 71,576
  • 32
  • 131
  • 164
  • Thanks! So I use multiple .htacces files(in different subfolders)? – Fdev Oct 10 '15 at 09:26
  • @Fdev If you wish. You should also go through: http://stackoverflow.com/q/20563772/1190388 – hjpotter92 Oct 10 '15 at 09:31
  • Thanks, it works so far. Seems like it works only into one direction (from long to short URL) I think I need another rewriterule to do the same job from short to long URL. PS: I don't need the base-element in HTML - just RewriteBase in .htaccess-file :) – Fdev Oct 10 '15 at 14:30
  • What do I have toDo with rewritebase when I upload the project to http://www.[myproject].com? Remove it? – Fdev Oct 10 '15 at 14:33
  • @Fdev Yes! Or, you can change it to `RewriteBase /` – hjpotter92 Oct 10 '15 at 14:46
  • I followed your link and it is very helpful - I will have to read it a few more times. Here is another try to rewrite another url: `//localhost/myproject/development/index.php?page=somepage&keyword=test&zip=12345&range=50` to `//localhost/myproject/development/somepage/test/53229/50` My solution looks like this: RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /index.php?page=$1&keyword=$2&zip=$3&range=$4 I also used a generator that gave me the same solution but this didn't work here. Maybe I have to count in your given rule and I have to rewrite this URL then? – Fdev Oct 17 '15 at 12:00