Directory structure change using .htaccess - php

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]

Related

.hatccess file to redirec to subfolder/subfolder

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]

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)

.htaccess - Remove "/demo/" from URL

i'm updating my site in new version for demo version.
I just keep my demo site as www.domain-name.com/demo/
Now i'm gonna keep my site in demo folder.. so if access www.domain-name.com it'll fetch file from "demo" folder
for that i used below coding.
RewriteRule ^(index.php)$ ./demo/ [L]
it will affect only root path other path it won't.
I need to change as below
from
http://www.domain-name.com/demo/index.php/contact-us
http://www.domain-name.com/demo/index.php/aboutus
....etc
to
http://www.domain-name.com/index.php/contact-us
http://www.domain-name.com/index.php/aboutus
Change the DocumentRoot (in the main server configuration)
DocumentRoot /foo/bar/demo/
For .htaccess this should work
RewriteEngine on
# redirect direct requests to /demo subfolder
RewriteCond %{THE_REQUEST} demo/
RewriteRule ^demo/(.*) http://www.domain-name.com/$1 [R=301,L]
# map everything else to subfolder
RewriteRule ^(.*)$ demo/$1 [L]
Do you say something like this?
RewriteRule ^(.*)$ /demo/$1 [L]

Symfony: Options for admin URL

In development mode my symfony admin/backend app can be accessed at http://localhost/backend_dev.php. For production mode, I created a controller php file, admin.php, so now in production the admin application can be accessed at http://www.domain.com/admin.php.
What do I have to do to allow the admin app to be accessed at domain.com/admin or admin.domain.com?
Thanks!
you can open new subdomain an on that subdomain (admin.domain.com) setup virtual host that points to server with your symfony app.
you can look at the full tutorial [here][1].
[1]: http://blog.mirthlab.com/2008/03/04/dynamically-loading-symfony-applications-via-subdomains/ here
You probably are better off putting everything admin like in the admin directory, but you can cheat by using mod_rewrite
RewriteRule ^admin/?$ admin.php [L]
Here are some basic ways you could do it:
Either dump admin.php into a folder called 'admin' in the root of www.domain.com, and rename admin.php to index.php. (Easiest solution)
Of course, this way you have to adjust all relative links in admin.php to one level up (appending '../' to the start of all relative urls should work), as well as all absolute links to reflect the changes.
Regarding your admin.domain.com, you should contact your webhost/domain name provider to setup a subdomain for you.
Or if your webhost allows .htaccess files, you could write a mod_rewrite rule.
i would create a module called admin...then in presumably the index action I would put whatever you had in your admin.php file.
then in your routing.yml file just point yourdomain.com/admin to the admin/index....that way you keep everything within the symfony front controller
Andrew
Make sure your DNS resolves the admin.domain.com correctly, then edit .htaccess in the /web to have mod_rewrite pick up on your subdomain and rewrite requests to admin.php. Optionally rename your admin.php to something less obvious or perhaps do a quick subdomain check inside it as well, or extend the rewrite with a 301 redirect if anyone hits domain.com/admin.php.
The following simple .htaccess works for me:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# The admin subdomain returns the backend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^admin\.domain\..*
RewriteRule ^(.*)$ admin.php [QSA,L]
# Check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# No?, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Change domain to your own domain.

Categories