.hatccess file to redirec to subfolder/subfolder - php

I am nto able to rewrite URL as I need.
I have the following document structure:
1) root - root folder where .htaccess should be placed in
2) root/folder1 - subfolder
3) root/folder1/public - this is folder where index.php is
Thus I need that mydomain.com would open index.php inside "public" folder. And all other requests would go via it.
I tried this, but it doesn't work (p.s. I am writing rewrite rules first time). I put it inside root folder.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{REQUEST_URI} !^/$1
RewriteRule !^folder1/public /folder1/public%{REQUEST_URI} [L,NC]

I have a similar .htaccess like yours, so I edited a bit mine. Does this worked for you?
The condiftion for .css|.js is needed otherwise it will send as plain/text instead of normal css/js
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js)$
RewriteRule ^([^?]*)$ root/folder1/public/index.php [NC,L,QSA]

Related

Load Multiple Urls as One Page

Not 100% sure how but I know its simple .htaccess but I have no idea what to do.
Basically I want it to load a index.php file no matter what url they go to in the folder containing the index.php for example:
http://website.com/folder/thisisntafile.php will load: index.php in the folder named folder. This will happen for whatever /folder/file.php is loaded.
Thanks!
Try this :
RewriteEngine on
#Rewrite "folder/file"
#don't rewrite "folder/index.php"
Rewritecond %{REQUEST_URI} !/index.php$
RewriteCond %{REQUEST_URI} ^/([^/]+)/[^.]+(\.php|html)?$
RewriteRule ^ /%1/index.php [NC,L]
This rewrites
http://example.com/foo/bar.php
to
http://example.com/foo/index.php

.htaccess to redirect parts of a URL to local files

I have a custom web app and the file structure works like this:
/apps/calendar/frontend/index.php
/apps/calendar/frontend/view/index.php
/apps/calendar/backend/index.php
/apps/calendar/backend/edit/index.php
/apps/calendar/backend/add/index.php
/apps/calendar/backend/view/index.php
I'm trying to write a .htaccess file to help redirect the files so they cant see the 'real' path.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^([^/\.]+)/(.*)/(.*)($|/$) /apps/$1/frontend/$2/$3 [NC,L]
RewriteRule ^([^/\.]+)/(.*)($|/$) /apps/$1/frontend/$2 [NC,L]
RewriteRule ^([^/\.]+)($|/$) /apps/$1/frontend/ [NC,L]
When I visit localhost/calendar it should map redirect to /apps/calendar/frontend/index.php. But when I visit localhost/calendar/add it gives me a 301 (permanent move) then shows the full page of localhost/apps/calendar/frontend/add/index.php in the console. Anyone got any ideas why this would happen? Or a better way around this? The apps might have heaps of sub-directories so, I'm not particularly keen on having a rule for ever subdirectory combination.
As you can see also I have a /admin path, which would load the /backend/ parts of the app. I would assuming I can do the similar code with the prefix of /admin?
This question might also be of your interest: Create blog post links similar to a folder structure.
Given that your .htaccess is located on the root folder of your domain /home/youraccount/public_html/.htaccess, it would look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(admin|apps) [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^([^/]+)/?(|.*)$ /apps/$1/frontend/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/apps [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^admin/([^/]+)/?(|.*)$ /apps/$1/backend/$2 [NC,L]
Let's say the user access:
http://domain.com/calendar
http://domain.com/calendar/
http://domain.com/calendar/add
All the above would redirect to
/apps/calendar/frontend/index.php
/apps/calendar/frontend/index.php/
/apps/calendar/frontend/index.php/add
And the if the user access:
http://domain.com/calendar/admin
http://domain.com/calendar/admin/
http://domain.com/calendar/admin/add
It would go to:
/apps/calendar/backend/index.php
/apps/calendar/backend/index.php/
/apps/calendar/backend/index.php/add
So it would make index.php your controller for each end:
/apps/calendar/frontend/index.php
/apps/calendar/backend/index.php

Redirect domains to path on main domain

I have some domains:
http://domainmain.com
http://domainone.com
http://domaintwo.com
My secondary domains are currently hosted under the main domain. No sub-directories, no other paths. So every domain get the contents of http://domainmain.com.
For better understanding: These files points all to the same file: http://domainmain.com/index.php, http://domainone.com/index.php, http://domaintwo.com/index.php.
For every domain I have a folder located at http://domainmain.com:
domainname folder / path
-------------- -----------
domainmain.com /
domainone.com /domainone
domaintwo.com /domaintwo
My goal is to redirect every domain to the corresponding dir / path http://domainone.com.
For example: http://domainone.com has to show the content of path /domainone. The visiter has to see http://domainone.com. This also should work: http://domaintwo.com/images shows http://domainmain.com/images.
Some code I started with in the .htaccess file:
RewriteCond %{HTTP_HOST} domainone.com [NC]
RewriteCond %{REQUEST_URI} !^/domainone
RewriteRule ^(.*)$ /domainone/$1 [NC,L]
And some PHP (but I want to use redirect instead of file_get_contents():
if ($_SERVER['SERVER_NAME'] == 'domaintwo.com') {
echo file_get_contents('http://domainmain.com/domaintwo');
die();
}
Note: It is only possible to have an .htaccess file at http://domainmain.com. My server runs PHP5.
Your question is quite similar to this one asked on the web-master's section;
How to redirect different domains to separate subdirectories.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(domainone)\.com$ [NC]
RewriteRule (?!^domainone(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]
RewriteCond %{HTTP_HOST} ^(domaintwo)\.com$ [NC]
RewriteRule (?!^domaintwo(/.*|)$)^.*$ /%1%{REQUEST_URI} [NC,L]

php mod rewrite .htaccess ignore directory

What rule should i set, to make the mod_rewrite ignore the directory "public" completely?
By that, I mean, the files should be accessible within it, but if the file does not exist, a server error page should come up with something like, FORBIDDEN, or FILE NOT FOUND what ever. I do not need custom error pages or stuff like that. I simply want the "public" to behave like there is no mod_rewrite at all.
Here is my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My file structure is
/system/
/application/
/public/
I want the folder public to behave, like there are no rewrite rules set at all, completely ignore it.
edit
That's my .htaccess file:
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I already had this .htaccess in the /public/ folder:
RewriteEngine off
I've tried all the different answers above (and a ton from google). I've tried to mix 'em up what so ever.
My folders:
/system/
/application/
/public/
/public/.htaccess #RewriteEngine off
/public/favicon.ico
/index.php
Below are the url with results I'm getting:
/public/favicon.ico -> I get the favicon
/public/faviDon.ico -> I get the index.php (without mod rewrite you would get "not found")
/public/ -> I get the index.php (without mod rewrite "forbidden")
So it still does rewrite urls, if the file was not found, or upon accessing a folder directly.
Can you se it?
Thank you very much for effort guys! I really appreciate it!
EDIT
I completely setup your files on my machine
// /.htaccess
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
.htaccess in the public folder:
// /public/.htaccess
Options -Indexes
RewriteEngine off
This disables rewriting like you wanted.
/public/ -> 403 Forbidden
/public/favicon.ico -> 200 File found
/public/not-existing.ext -> 404 File not found
Do you have a index.php in you public folder?
Maybe you could remove that one..
What kind of machine your testing on?
I tested it on Linux + Apache 2 + PHP5.3
I can give you more support in the afternoon (my time +2 GMT)
EDIT 2
When I remove this line from /.htaccess is still works
RewriteRule ^(public)($|/) - [L,NC]
Everything is handled by the .htaccess in the public folder.
Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)

Directory structure change using .htaccess

I want to move all my web site files (even including index.php) into a subdirectory (for exp: "abc")
For example
BEFORE:
public_html
index.php
a.file
directory
an.other.file
...
AFTER:
public_html
abc_directory
index.php
a.file
directory
an.other.file
...
I want everything to work, as it was before, but i don't want to make any redirections (visible).
People should enter "http://myexmaplesite.com/directory/an.other.file/" and by .htaccess apache serve them "http://myexmaplesite.com/abc_directory/directory/an.other.file/" BUT WITHOUT EXTERNAL REDIRECTS (301,302 etc.)
How could I route all requests to a subdirectory using mod_rewrite?
Try this mod_rewrite rule in your document root:
RewriteEngine on
RewriteRule !^directory/ directory%{REQUEST_URI} [L]
Or in general:
RewriteEngine on
RewriteCond $1 !^directory/
RewriteRule ^/?(.*) directory/$1 [L]
This should be even applicable in server or virtual host configurations.
Something like
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ directory/$1 [L,QSA]

Categories