1

I'm trying to make a backend for a site to work with URL friendly. What I'm trying to accomplish is that everything after http://account_name/backend will be redirected to index.php (and that file will take care of the querystring), but if a user is not logged in, index.php will redirect to login and that URL should point to a login.php file.

At this point if I hit http://account_name/backend, I'm being redirected to http://account_name/backend/login and I see the login.php file, but the js and css recources are not loaded. This resources are beeing called like:

<script type="text/javascript" src="js/jquery.js"></script> 

Obviously I'm having some path issues, but I can't figure it out. I've tried setting RewriteBase in htaccess and base href in HTML but with no luck.

Also, I should mention that if try to access a js directly I'm being redirected to

http://account_name/backend/js/jquery.js > http://account_name/backend/js/login 

The files for the front are in:

D:\FolderA\FolderB\AccountName\Project 

The files for the backend are in:

D:\FolderA\FolderB\AccountName\Project\backend 

This is my virtual host:

<VirtualHost account_name> DocumentRoot "D:\FolderA\FolderB\AccountName\Project" <Directory "FolderA"> AllowOverride All </Directory> </VirtualHost> 

This is my .htaccess so far:

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{ENV:REDIRECT_STATUS} !^[\s/]*$ RewriteRule ^ - [L] RewriteRule ^login$ login.php [L] RewriteRule ^(.*)$ index.php?route=$1 [L,QSA] 
3
  • How is the user redirected to <path>/login pages? None of the given rules is supposed to be doing that. Commented Sep 19, 2015 at 16:42
  • @hjpotter92 inside index.php, if session not set: header("location: login"); Commented Sep 19, 2015 at 16:49
  • @hjpotter92 I can also type http://account_name/backend/login in the address bar Commented Sep 19, 2015 at 16:50

2 Answers 2

1

Update the last rule to recheck file existence:

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?route=$1 [L,QSA] 
Sign up to request clarification or add additional context in comments.

Comments

1

Actually problem is your your first rule that is doing ANDing rather ORing

# if request is for a directory AND RewriteCond %{REQUEST_FILENAME} !-d # if request is for a file AND RewriteCond %{REQUEST_FILENAME} !-f # if request has been rewritten once RewriteCond %{ENV:REDIRECT_STATUS} !^[\s/]*$ RewriteRule ^ - [L] 

Obviously this rule will never succeed as one request cannot be for a file and and a directory at the same time that has already been rewritten (%{ENV:REDIRECT_STATUS} check).

Due to this rule failing your rule is actually looping and being rewritten like this:

  1. backend/js/jquery.js => index.php?route=backend/js/jquery.js
  2. index.php?route=backend/js/jquery.js => index.php?route=index.php

After step 2 mod_rewrite stops as source and target are same i.e. index.php.

Code inside your index.php then redirects to login as you mentioned in your comment above.

To prevent this behavior you can use rule as suggested by @hjpotter92 above or fix your first rule by using OR between conditions:

RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{ENV:REDIRECT_STATUS} !^$ RewriteRule ^ - [L] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.