2

I'm close, to my final solution I think. The .htaccess looks like this:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /myproject/development/
RewriteRule ^((?!index\.php)[^/]+)/?$ index.php?page=$1 [L]
RewriteRule ^((?!index\.php)[^/]+)/([A-Za-z0-9]+)/([0-9]+)/([0-9]+)/?$ index.php?page=$1&keyword=$2&zip=$3&range=$4 [L,R]

I don't need the RewriteBase for the 1st rule(was a little surprised about that) but I need it if I add the 2nd rule and open this URL:

//localhost/myproject/development/somepage/test/13245/50

Otherwise the page will be opened but of course without the stylesheets and javaScripts can be found then.

1.) Target: I want to use the 2nd rewriteRule without changing or adding a rewriteBase. What do I need to change in the .htaccess so I can keep testing my project without a rewriteBase.

Why: As I asked before I want to test my project locally and on the live-server without changing too much on the project configuration.

2.) The [R] Flag If I request

//localhost/myproject/development/somepage/test/53229/2000

Of course in the adressline then we have this URL

//localhost/myproject/development/index.php?page=somepage&keyword=test&zip=12345&range=2000

To avoid this behaviour I simply should remove the R-Flag. But then the CSS and JS can't be found anymore. Also Here I'm looking for a solution without rewritebase, basepath, virtual host, etc. if possible.

Here is where I started: Rewrite rules for localhost AND live envoirement

Community
  • 1
  • 1
Fdev
  • 63
  • 1
  • 6

1 Answers1

1

By the looks, you don't want the R flag on the 2nd RewriteRule. That defeats the object of your "pretty" URLs.

But then the CSS and JS can't be found anymore.

Because you are using relative paths to your CSS and JS files. Either change your paths to root-relative (starting with a slash), or use the base element in the head section of your pages, to indicate the URL that all relative URLs are relative to:

<base href="http://www.example.com/page.html">

More Information:
https://developer.mozilla.org/en/docs/Web/HTML/Element/base


I don't need the RewriteBase for the 1st rule(was a little surprised about that)

You don't need the RewriteBase for the 1st rule (an internal rewrite) because the directory-prefix (the filesystem path that lead to this .htaccess file) is automatically added back on relative path substitutions.

However, for external redirects (ie. R flag), the directory-prefix does not make sense, so you either need to specify a root-relative (starting with a slash) or absolute URL. Or specify the appropriate RewriteBase directive, which overrides what URL-path will be added for relative substitutions (that's all it does).

MrWhite
  • 43,179
  • 8
  • 60
  • 84