URL Rewrite problem. (Many directory) - php

I'd like to make a htaccess file, which can make a good structure for my websites.
My .htaccess is now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|gif|png|css|js|pl|txt)$
RewriteCond %{REQUEST_URI} !/admin
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
(based on Sombat's comment, and about 30 try :P)
And I want to make this, with it:
for every elements but (jpg|jpeg|gif|png|css|js|pl|txt)
if domain.xx/admin redirect to the domain.xx/admin directory and don't make a rewrite at all
i mean: let me use domain.xx/admin/index.php?asd=1&asdd=2
else rewrite everything as rule one, to index.php.
Thanks for the help.

You can just add another RewriteCond directive after the one you have now that will exclude the admin directory.
RewriteCond %{REQUEST_URI} !^/admin/.*
That will prevent your RewriteRule from being applied if the RewriteCond matches your admin path. The order of RewriteCond and RewriteRule directives is important, so be sure to put it before the RewriteRule that you want it to affect.

Related

htaccess adding [NC,F] to GET parameter

I'm working on an MVC project and I have the following .htaccess file:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [L]
RewriteRule !^(public/|index\.php) [NC,F]
It works OK. I only want the public/ folder and the index.php file to be accessible to the public. All other paths should be inserted into the path GET parameter. For example, mysite.com/controller/method should point to mysite.com/index.php?path=controller/method.
Now, there is a problem. When visiting the URL directly (without including index.php, it is adding [NC,F] to the GET path parameter. It's like visiting mysite.com is pointing to mysite.com/index.php?path=[NC,F].
Why is this happening and how do I fix it?
EDIT
I moved index.php into the public/ folder. Here is my .htaccess file now:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
It seems to work OK. Are there any other improvements I could make on this?
You don't have a redirect location on the last rule, so it's taking the flags as the redirect location. Just a dash will be fine since it's a forbidden response. Change the last line to:
RewriteRule !^(public/|index\.php$) - [NC,F]
Also adding the dollar sign after index.php just to be clear.
Edit:
I would suggest updating your new rule set to the following (actually I suggest a complete re-think below, but this is an update on what you have):
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule !^(public/) - [NC,F]
The (/)? wasn't needed in your homepage rule, as the opening forward slash is not included in .htaccess matches anyway.
I moved your rule for the homepage to the top or it will never be used due to being matched by the previous rule (so the path param is not there when empty, which is presumably what you intended).
I stopped anything in /public/ from being passed to your index.php script, since the way you had it, anything in public that didn't exist would have been passed to your index script, which does not seem to be what you intend.
I added RewriteCond %{REQUEST_URI} !=/public/index.php so the rule couldn't be executed on itself and create a loop if rule processing is run through more than once, which it can be, but then took it back out because the above match on /public/ covers that anyway.
A Re-Think
All that said, I don't think it really makes sense to check if files don't exist and then just send forbidden responses to the ones that do, yet send everything else to your index script. Why not just send everything to your index script? That seems to be what you want really. I would suggest you simplify to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
Dropping the homepage rule since no need to worry about an empty path parameter being passed to your index script. Changing the logic to be "Leave anything in /public/ alone. For anything else, pass it through to the index.php script." so files tests not needed since the script handles it all, and no forbidden response needed because there is nothing left to match, it's all covered by the rules. You can always return forbidden to anything you don't want to process in your script, which you would have needed to do anyway for existing file URLs in your previous setup.
One Last Re-Think
And finally, if I might suggest, it would be cleaner to have your index.php file in the root of the website, so you can make /public/ work with its own index file later if you like, so finally I would move it back to the root and change the rules to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?path=$1 [L]
And if you like all that, an up-vote to go with already accepting the answer would be much appreciated. :)
Adding RewriteRule ^(/)?$ public/index.php [L] seems to have resolved the issue. I'm not sure if this is the best approach, but here is my .htaccess file now:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
I moved index.php into the public folder to make things clearer.

.htaccess - Redirect subdomain to folder

This question has probably been asked for over a thousand times, but I've tried so many scripts, and googled so long while finding nothing, I thought, let's just ask.
I simply want m.daltonempire.nl to be redirected to daltonempire.nl/m/ without the user seeing the URL change.
So if m.daltonempire.nl/hello.php is requested, I want the user to keep seeing this URL, while the page given is actually daltonempire.nl/m/hello.php.
Note: I do not want www., so simply http://m.daltonempire.nl
Thanks in advance,
Isaiah v. Hunen
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^m\.daltonempire\.nl$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ m/$1 [L]
The %{REQUEST_FILENAME} conditions would let you access /exists.php and not rewrite it to /m/exists.php. Remove those two if you want to rewrite even if that may potentially override existing files and directories.
Try this example:
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteRule (.+)$ "http://example.com/%1" [L,P]
Any requests http://test.example.com will be mapped to http://example.com/test/...
Try googling dynamic subdomain with php and htaccess to get better search results.
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/ under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com to mybb installation in folder /forum/mybb/ so the .htaccess need to put [L] on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.

.htaccess mod_rewrite check if querystring var is avail

Basically I want to rewrite my urls so that it is website.com/folder/ sometimes though I need it to rewrite also website.com/folder/page/
Currently I have it working with just the website.com/folder/ but can not get it to check if there is a page, if I create just another rule under the folder one it reads that one, and gives me an empty page var, which is breaking my php. I struggle with .htaccess and any help would be appreciated.
Here is what I have that works with just the folder but I can not include a page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|docs)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
Here is what I tried to get it to work with either just a folder, or a folder and page
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)/$ /?folder=$1&page=$2 [L,QSA]
Please Help!
Accordingly to the RewriteRule docs you should reverse the rules order in your rules set. Because in your configuration both rules have the same RewriteCond, the most specific rule (folder + page) should be atop and the most general rule should be the last one. If not when the first rule is matched the URL is rewritten and the second rule never matches. Also, probably you want to remove the trailing forward slash in the pattern of your folder + page rule (assuming that the second group in the pattern matches a page not a folder). So I think the whole thing should read:
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/([^/]*)$ /?folder=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !^/?(css|js|images|html|doc)/
RewriteRule ^([^/]*)/$ /?folder=$1 [L, QSA]

.htaccess if statement?

I haven't worked with .htaccess too much. Looking to do something like this:
When user types in domain.com/path it is then pulled from domain.com/index.php/path/to/file
UNLESS
someones goes to domain.com/admin
How can an add a exception or if statement for a few words that i dont want to redirect?
Put a rule for the admin rewrite first and use [L] at the end to tell .htaccess this is the last rule it should follow. Then, put your other rules after it.
Example:
RewriteRule ^admin(/?)$ - [L]
RewriteRule ^(.*)$ /path/to/file.php?id=$1
Using the - after the match rule means it won't rewrite it, it'll just go to the path that was provided.
You need to add a RewriteCond rule to exclude stuff from the rewrite rule.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin
RewriteCond %{REQUEST_URI} !^index.php
RewriteRule ^(.*)$ /index.php/$1 [L]

Tricky .htaccess and Drupal problem

1) I have a Drupal site located at http://example.com and I have a directory located at http://example.com/foo, but I also have a Drupal page with an alias of http://example.com/foo. How can I get the Drupal page to serve? Currently, I get a 403 forbidden page as a result of the Options -Indexes declaration in Drupal's .htaccess file, but I do not want to remove this as I do not want directories to be browsable.
EDIT: I have solved this with the following rule:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
2) To make the problem even more difficult, given the same scenario, if I have an index.html file inside the directory http://example.com/foo/index.html I always want this to take priority over Drupal's aliased page (which it does at the moment - currently I have modified the .htaccess file so that any directory with an index.html file displays it - DirectoryIndex index.php index.html).
EDIT:
So now, how can I write a RewriteCond that will look to see whether or not there is an index.html file inside the directory?
I am no RewriteRule Guru and pretty sure there is a nicer way to achieve this, e.g. with conditional RewriteCond.
But simply adding
RewriteCond %{REQUEST_URI} =/foo/
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Above the default rewrite Conds and rules make this rule kick in for /foo/ and not the Drupal-default one. Where a RewriteCond %{REQUEST_FILENAME} !-d will only apply the RewriteRule if something is NOT a dir.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteRule ^(.*)$ http://localhost/s2k/index.php?q=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}index.html -f
RewriteRule ^(.*)$ $1index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*)$ $1/index.html [L]
[normal Drupal rewrite rules]

Categories