Matías Cánepa
Matías Cánepa

Reputation: 5974

htaccess and URL friendly

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]

Upvotes: 1

Views: 68

Answers (2)

anubhava
anubhava

Reputation: 786021

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]

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80657

Update the last rule to recheck file existence:

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

Upvotes: 1

Related Questions