0

I've got a little problem with URL rewriting in my .htaccess running on my local apache. I configured the entry point of my server via symbolic link (localhost/memap/), so "memap" leads to my project root (no vhost configured).

I'm not very familiar with url rewriting, but redirecting all requests to my index.php works fine. The index.php leads to a custom request controller, that processes the request and returns the according page.

My problem now is that no js,css etc. gets loaded. I tried several RewriteRules, but don't get a solution. This is my current .htaccess file, that doesn't contain any rule for js,css etc. yet:

# Allows ModRewrite to work
Options FollowSymLinks

# Turn on rewrite engine
RewriteEngine On
RewriteBase /memap

# Redirect all requests to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^page/(.*)/(.*)$ index.php?page=$1&id=$2 [NC,L,QSA]
RewriteRule ^page/(.*)$ index.php?page=$1 [NC,L,QSA]

If I'm not mistaken, the rewrite conditions should lead requests for files and directory, without actual rewriting, directly to its target in case the target file/directory exists.

Unfortunately this doesn't happening. E.g. my css-file (memap.css) is reachable via "localhost/memap/css/memap.css", but the server looks up on "localhost/css/memap.css" (without my memap symlink). Accordingly the same happens with my js ("localhost/js/memap.js"). This is how include them in my html:

<script src="js/memap.js"></script>
<link rel="stylesheet" href="css/memap.css" media="screen" />

I tried a lot of things, but don't know how to achieve my goal. Does anyone have a solution to this? It's probably just a minor thing that's missing...

Thanks in advance!

buddy274
  • 1
  • 1

1 Answers1

0

Your RewriteConds only apply to the first RewriteRule.

You can write two time the RewriteCond block:

RewriteBase /memap/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/(.*)/(.*)$ index.php?page=$1&id=$2 [NC,L,QSA]

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

Or you can skip two RewriteRule with [S=2]:

RewriteBase /memap/

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [S=2]
RewriteRule ^page/(.*)/(.*)$ index.php?page=$1&id=$2 [NC,L,QSA]
RewriteRule ^page/(.*)$ index.php?page=$1 [NC,L,QSA]

And you should write, for your JS & CSS files:

<script src="/memap/js/memap.js"></script>
<link rel="stylesheet" href="/memap/css/memap.css">
Shiva127
  • 2,413
  • 1
  • 23
  • 27
  • Thanks for the hint. But unfortunately this does not fix my actual problem. My css and javascript files don't get loaded on my website, because the path is still wrong :( – buddy274 Apr 25 '17 at 13:34
  • I updated my answer. Everiting will be redirected to `/memap/*`. – Shiva127 Apr 25 '17 at 13:43
  • I tried both version of yours, but still: "localhost/css/memap.css" instead of "localhost/memap/css/memap.css". I just don't get it :( – buddy274 Apr 25 '17 at 14:40
  • Where did you see the `localhost/css/memap.css`? Cause `RewriteRule ^(.*)$ $1 [L]` should redirect it to `/memap/css/memap.css`. – Shiva127 Apr 25 '17 at 14:44
  • My chrome inspector says so. In the console of the inspector I see all GET-requests that are failing. And of course I see that my css is not available on my homepage. – buddy274 Apr 25 '17 at 15:35
  • Alright, I got it now for my index page. How should I construct links to another page? 1. "index.php?page=test" or 2. "page/test"? If I take option 1, then my css loads as it should, but the adress in the browser isn't rewritten (still "index.php?page=test"). When I try option 2, then my css is missing again and the css path looks like this: "localhost/memap/page/css/memap.css" which is wrong. Does anyone know a solution? – buddy274 Apr 25 '17 at 17:51
  • Use option 2: `page/test`. I updated my answer for the JS & CSS files. – Shiva127 Apr 25 '17 at 19:25
  • Alright, thank you, this seem to work. But do you know why this problem appears? I mean it is not very elegant to use the server configuration within the code. Just in case I'd move this to another server with a different path, i have to adjust my code to make it work. – buddy274 Apr 26 '17 at 07:57
  • No, it's not, but it's the problem when you set your website in a subpath, use URL rewriting and [relative path](http://stackoverflow.com/a/24028813/1737263). Because with the URL rewriting, the browser think he is in a subdirectory and call the file accordingly to that subdirectory (when called with a relative path, no leading `/`). – Shiva127 Apr 26 '17 at 09:02