.htacess queries and page links


#1

I have a couple of questions regarding how .htaccess works.

1 Does htaccess affect SRC linked files. for example, would a rewrite rule that redirects all rquests to /index.php prevent also redirect the theme/css/style.css in the style tag of index.php to /inxded.php, or does the redirection only affect the URL entered in the address bar of the browser?

  1. I was trying to write a set of conditions that would do the following:
    • Redirect requests to index.php if:
  • the requested url is a directory AND does is NOT named ‘engine’ OR ‘content’
  • the requested url is a directory AND does not exist
  • the requested url is a file AND does not exist
  • the requested url is a file that exists and has the suffix ‘.php’ OR ‘.html’ OR ‘.htm’ OR ‘.asp’

in short, any url except ‘/engine’ would cause a rewrite.

I came up with this… but it fails to rewrite anything :confused:

RewriteEngine On
#redirect non existent pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#redirect all directory request EXCEPT ‘engineFolder’
RewriteCond %{REQUEST_URI} !^/engineFolder [NC,OR]
#redirect all file request that end in ‘.php’,’.html’,’.htm’,’.asp’
RewriteCond %{REQUEST_URI} /[\w]+.(php|html|htm|asp)$ [NC]
RewriteRule .* index.php [L]

as always, any advice is appreciated


#2
  1. RewriteRule only changes what is served for a URL entered in the browser, it does not modify your HTML in any way whatsoever

  2. Not really sure what you’re after. First you say “‘engine’ OR ‘content’”, then “in short, any url except ‘/engine’ would cause a rewrite.” and the code you’ve provided is for “engineFolder”. Which is it? :slight_smile:

Also, a file cannot exist and not exist at the same time, bullets 3 and 4 are contradicting each other. Sounds like you maybe want 4 separate RewriteRules instead of one? All RewriteCond’s are combined with AND, so now it’s a list of 4 conditions that all need to be met before the RewriteRule fires.


#3

It depends how the redirect is set. If its domain specific. All the path will redirect.
IF its file specific. Then it will not redirect in case of other files hosting on the server.


#4

ScallioXTX,

  1. I was asking because, i have come close to making the redirect work like i would like, but when it goes to the page none of the styles of .js is present ; I figured it could a problem with the links in the header which are relative.

  2. I mean the directory with the htacess file contains 4 items by default: index.php, engine, themes , and content. unless the user is trying going to the engine directory, the user should be redirected to the index.php file.

2-a) Incidentally, my original attempt only contains the conditions:

RewriteCond %{REQUEST_URI} !^/engineFolder [NC,OR]
#redirect all file request that end in ‘.php’,’.html’,’.htm’,’.asp’
RewriteCond %{REQUEST_URI} /[\w]+.(php|html|htm|asp)$ [NC]

I though the spec stated that [OR] joined the next conditions an OR. ( else , how would you go about writing OR condition)

but that didn’t cause any redirects at all. Perhaps I have a typo in my logic? I am unfamiliar to how the regex for htacess behaves.

the decision tree I would like to follow is:
rewrite any directory requests(to existing or non existing directories) to index.php UNLESS it’s the ‘/engine’ directory
if the URL is a file (without query string) ends in .php,.asp, .html, or .htm rewrite to index.php


#5
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/engine [NC,OR]
#redirect all file request that end in ‘.php’,’.html’,’.htm’,’.asp’
RewriteCond %{REQUEST_URI} \.(php|html|htm|asp)$ [NC]
RewriteRule ^ /index.php

Could you try that please?


#6

Thanks Scallio…

It ALMOST worked. The one thing what went wrong with this solution, the thing thats burning my noodle, is that the stylesheets and scripts are still not linking (so it does navigate to index.php, which then runs an include of the expected page, but no CSS is loaded )

I was fiddling with this less than graceful solution:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_URI} !engine/?.*
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_URI} \.(html|php|htm|asp|rmc)$ [NC]
RewriteRule .* index.php [L]

It works perfectly, but it seems like a LOT of repetition. :confused:


#7

The repetition is pretty typical for .htaccess files. Nothing to worry about.

I would however keep a newline between blocks for readability

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* index.php [L]

RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_URI} !engine/?.*
RewriteRule .* index.php [L]

RewriteCond %{REQUEST_FILENAME} -f 
RewriteCond %{REQUEST_URI} \.(html|php|htm|asp|rmc)$ [NC]
RewriteRule .* index.php [L]

Two small changes I would also suggest is replace RewriteRule .* index.php [L] with RewriteRule ^ index.php [L] as that is (marginally) faster.

Also, you might want to add a start of string anchor to your engine directory to prevent it from matching a directory like motor-engine:

RewriteCond %{REQUEST_URI} !^/engine/?

(The .* you had at the end is not needed, just the initial match should be enough)