Jump to content

.htaccess File - Causing Loops?


devilindisguise
Go to solution Solved by devilindisguise,

Recommended Posts

Hello all

 

Was hoping someone could help. I have a .htaccess file that contains the following:

RewriteEngine On

RewriteRule ^sites/([a-z0-9]+)/?$           sites.php?letter=$1 [NC,L,QSA]
RewriteRule ^network-devices/([a-z0-9]+)/?$ network-devices.php?letter=$1 [NC,L,QSA]
RewriteRule ^site/([0-9]+)/?$               site.php?site_id=$1 [NC,L] 
RewriteRule ^network-device/([0-9]+)/?$     network-device.php?device_id=$1 [NC,L] 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php?%{QUERY_STRING}

The problem I am having is that if I try to restrict access to a page via the following code I get the following error from Chrome:

 

Code:

if(!isset($_SESSION['username'])) { header('location: login'); }

Chrome Error:

This webpage has a redirect loop

 

I also get a similar error on other browsers so leads me to think that something strange is afoot in that .htaccess file.

 

So for example, I can restrict access quite nicely to my index.php page. If I try enter it into my URL I get whisked away to:

http://localhost:8888/<SITENAME>/login

If I then try to browse to an area of the site called 'sites' I get the loop and see that the URL has now changed to:

http://localhost:8888/<SITENAME>/sites/login

Pretty sure that it should rather read:

http://localhost:8888/<SITENAME>/login

Is it immediately obvious to anyone as to what the issue could be?

Thank you

Link to comment
Share on other sites

Why you're going to /sites/login is straightfoward: you gave the browser the relative location "login" so if you're trying to go to /sites/123 then it will take you to /sites/login. If you went to /sites/123/, with the trailing slash, then you'd be redirected to /sites/123/login.

You need an absolute URL in there so you don't have to worry about paths.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.