Excluding a script from the general UrlRewrite rules - .htaccess

I have following rewrite rules for a website:
RewriteEngine On
# Stop reading config files
RewriteCond %{REQUEST_FILENAME} .*/web.config$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .*/\.htaccess$ [NC]
RewriteRule ^(.+)$ - [F]
# Rewrite to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(/bilder_losning/|/bilder/|/gfx/|/js/|/css/|/doc/).*
RewriteRule ^(.+)$ index.cfm?smartLinkKey=%{REQUEST_URI} [L]
Now I have to exclude a script including its eventually querystrings from the above rules, so that I can access and execute it on the normal way, at the moment the whole url is being ignored and forwarded to the index page.
I need to have access to the script shoplink.cfm in the root which takes variables tduid and url (shoplink.cfm?tduid=1&url=)
I have tried to resolve it using this:
# maybe?:
RewriteRule !(^/shoplink.cfm [QSA]
but to be honest, I have not much of a clue of urlrewriting and have no idea what I am supposed to write. I just know that above will generate a nice 500 error.
I have been looking around a lot on stackoverflow and other websites on the same subject, but all I see is people trying to exclude directories, not files. In the worst case I could add the script to a seperate directory and exclude the directory from the rewriterules, but rather not since the script should really remain in the root.
Just also tried:
RewriteRule ^/shoplink.cfm$ $0 [L]
but that didn't do anything either.
Anyone who can help me out on this subject?
Thanks in advance.
Steven Esser
ColdFusion programmer

Please try to put the following line at the top of your config (after RewriteEngine on):
RewriteRule ^shoplink.cfm$ - [L]

Related

URL Rewriting using .htaccess

I'm trying to get a url to rewrite using htaccess but can't seem to get it working.
I'm trying to rewrite http://website.com/pages/blog/article.php?article=blog-entry so that it can be entered as http://website.com/pages/blog/blog-entry but i'm getting an error when I try the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1 [NC,L]
Can anybody see where i'm going wrong as this just gives me a 404 error. Thanks in advance.
Use this rule inside /pages/blog/.htaccess:
RewriteEngine on
RewriteBase /pages/blog/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ article.php?article=$1 [QSA,L]
I'm trying to rewrite
http://website.com/pages/blog.php?article=blog-entry so that it can be
entered as http://website.com/pages/blog/blog-entry but i'm getting an
error when I try the following:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond
%{REQUEST_FILENAME}\.php -f
RewriteRule ^pages/blog/(.+)$ pages/blog/article.php?article=$1
[NC,L]
Your wording is confusing, but I believe this is what you mean:
The real url is: http://website.com/pages/blog.php?article=blog-entry
you want to be able to use a 'friendly' url: http://website.com/pages/blog/blog-entry to point to the real url.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^pages/blog/(.+)$ /pages/blog/article.php?article=$1 [QSA,L]
The first two tests ask: is this a directory that exists? is this a file that exists? Because article.php is a file, it won't be included in this action, so you won't enter into an endless loop, which is always the risk with incorrectly done rewrite rules.
Take the given url, and use query string append (QSA) to attach the desired data to the actual file that will process the request. This is not a rewrite in that the url the user sees does not change, this only happens internally in apache, which sends the request to the desired target, with the desired information.
You have to test if the file or directory exists because otherwise you'd be applying this rule incorrectly, since it should only be applied when the target does NOT exist. This is basically how all blog/cms 'search engine friendly urls' work, more or less.
Last, since the target is /blog.php?article=blog-entry you can't skip the leading /.
However, it's unclear to me why you'd want the friendly url to be so long, when you can just make it short, and friendlier: like, pages/[article-name]

.htaccess - alias domain root to subfolder

I've got an install of concrete5 living in my site root - the problem is that the C5 file structure is pretty varied, and it's getting mixed in with my subdirs and subdomains. I can't tell what's my code and what's C5, I hate it.
My solution is to move everything to a /_concrete/ folder - but since my domain has to point to the root, I can't use the files here.
Enter .htaccess: I need to write a script to redirect any instance of www.domain.com (ignore subdomains) to www.domain.com/_concrete/ - but not a transparent 301, just an alias. To clarify: www.domain.com/page/ and www.domain.com/_concrete/page/ should display the same thing. www.domain.com/page/ should NOT change it's URL to www.domain.com/_concrete/page/.
I've adapted this from a similar script I use elsewhere:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /_concrete/$1 [L,QSA]
But this only causes a server error. I hope I'm close - can anyone point me in the right direction?
You're telling the server to check if the value is not a filename, but you're not having it ignore the _concrete directory itself (which would also match your script and create a loop).
This might get you closer to what you're trying to do.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !^_concrete
RewriteRule ^(.*)$ _concrete/$1 [L,QSA]
Additionally you might want to have it ignore directories that exist as well. At which point _concrete would no longer be matched because it exists.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _concrete/$1 [L,QSA]

RewriteRule For Matching Arbitrary PHP Files

I'm somewhat new to htaccess rewrite rules, and have been scratching my head for the past few days on what's happening here. No amount of Googling seemed to help, so hopefully somebody knows the answer.
I have a site that can be accessed as:
www.site.com
www.site.com/684
www.site.com/684/some-slug-name-here
All of these scenarios should go to index.php and pass in the optional id=684 and slug=some-slug-name-here
Which works fine.
My problem is I have a separate file. Right now it's called admintagger.php - but this fails when I call it anything. 21g12fjhg2349yf234f.php has the same issue.
The problem is that that I would like to be able to access admintagger.php from www.site.com/admintagger
but it seems to be matching my rule for index, and taking me there instead.
Here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^imagetagger$ /imagetagger.php [NC,QSA]
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
If you want to arbitrarily be able to access php files via the name (sans extension) then you need to create a general rule for it. But you need to be careful otherwise you may be rewriting legitimate requests for existing resources (like a directory, or a slug). Try this instead:
# make sure we aren't clobbering legit requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# see if appending a ".php" to the end of the request will map to an existing file
RewriteCond %{REQUEST_FILENAME}.php -f
# internally rewrite to include the .php
RewriteRule ^(.*)$ /$1.php [L]
Then you can have your routing to index.php right after that:
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
Although you may be better off create a separate rule for each of your 3 cases:
RewriteRule ^([0-9]+)/([^/]+)/?$ /index.php?id=$1&slug=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [NC,L,QSA]
RewriteRule ^$ /index.php [L]

Possible .htaccess errors, 1and1.com not rewriting

The following .htaccess file works perfectly on my local server.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.cssc
RewriteRule . style.php [L]
RewriteRule ^admin\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . admin.php [L]
I am doing some work for a client and he is using 1and1.com. I do not know anything about his account or what package he has.
All files are rewritten to admin.php (unless they actually exist). The problem is, I am getting a 404.
I know it's reading the .htaccess file because:
I can put garbage in the file and get a 500 error.
If I do a general rewrite (all pages go to admin.php), it works.
Also, it seems that 1and1 does it's own rewriting. If I go to: http://somewhere/afile, it will include afile.js even though I am not requesting the .js.It is super strange.
Does anyone have any experience with this? Or any insight?
To add some information..
1and1 seem to have some sort of default rewriting already in place that can interfere with your own rules - for example:
RewriteRule ^product$ /product.php [L]
It gave a 404 for this, and also for any other rewrite rule that matched a pre-existing .php file in my root folder.
2 things that 'fixed' it:
Change rewrite text to no longer match filename:
RewriteRule ^productz$ /product.php [L]
Or, change filename:
RewriteRule ^product$ /product_.php [L]
Both work - both very annoying.
The answer was simply "Do not nest .htaccess files". I had one in a sub folder.

mod_rewrite and .htaccess - stop rewrite if script/file exists

This is my htaccess:
RewriteEngine On
# Stop if it's a request to an existing file.
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
# Redirect all requests to the index page
RewriteRule ^([^/]) /index.php [L]
Now this forwards everything to my index.php script! It dosen't stop if a script exists. Anyone have any idea why this isn't working? I've looked everywhere, but I don't really understand mod_rewrite (as much as I thought!).
The problem has come about because I've put in <script> tags which point to .js files in my web directory which are then forwarded to the index.php script. The web developer toolbar tells me this. :) And clicking links to the js files in firefox's view source window also shows the index.php output.
thank you.
This is because after processing a rewrite rule the whole process restarts with the new url. The processing of an url can go thru the rules over and over again each time with the changed URL, until there is no more change (no applying rules found).
You need this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]
Don't think of the rules as a program, they are rules which can overlap and must be as specific as possible with each one.
I guess you should add [OR] after the first condition like this:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1 [QSA,L]

Resources