I'm trying to use a sub-directory as the root folder for one of my domains. Using .htaccess, I use mod_rewrite's to get the job done. Here's the code I already have:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]
This gets the job done, but not entirely. For example:
If I goto http://domain.com/, it displays index.php from inside the domain folder. If I goto http://domain.com/test/, it will display the contents (or 403) of the test folder. BUT if I goto http://domain.com/test (for use of shortcuts, or even to display the folder) I get redirected to http://domain.com/domain/test/.
That is not supposed to happen. If anything, it either does a mask from the .htaccess (if test is being used) or should just goto http://domain.com/test/. I have tried to figure out a way around this, and I cannot. So I am seeking your help! :)
Any and all help is greatly appreciated.
Try this: its a little crude, but should do what you want. There is a little bit of confusion: some of what I've tried to do will depend on your RewriteBase (you might need to remove one or more / characters).
I've basically added an initial block that specifically looks for directories within your /domain/ folder that don't end with a trailing slash and added one. Let me know if it works at all.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /domain/$1/
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]
The commenter is right, if you want to use a sub-directory as the root folder for one of my domains , just configure it in you apache virtual host configuration (DocumentRoot).
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(.*)$ /domain/$1/ [L]
The above code successfully does everything I wanted it to do.
Use RewriteBase like this:
RewriteBase /f2f/f2fweb
What helped me is auto.htaccess generator on my host!!!!
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain.com/TEST//.*$ [NC]
RewriteRule \.(js|css|jpg|gif|png|bmp|mp4|3gp|m4a|m4r|aac|mp3|ogg|wave)$ - [F]
Related
I am through .htaccess redirecting my domain to a subfolder in my public_html folder like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/mydomain.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ mydomain.com/php [L]
When writing mydomain.com/index.php everything just works fine but if i leave out the index.php and just writes mydomain.co I get an 404 error?!?
What am i missing?
Any help is appreciated and thanks in advance :-)
Change the ^(.*)$ pattern to ^(.+)$.
Or, switch the order of your rules.
Have you tried removing the trailing slash from
RewriteCond %{REQUEST_URI} !^/mydomain.com/
To make it
RewriteCond %{REQUEST_URI} !^/mydomain.com
?
I am trying the following in htaccess and everything works except one detail: It posts the whole /var/www/project path instead of just giving me what I think I am telling it to give me.
When I type dev.mysite.com/folder
it correctly gives me http://dev.mysite.com/index.php?path=folder, as expected.
However, in the other case, I am expecting this: http://dev.mysite.com/index.php?path=main.php&u=1079
but instead it gives me http://dev.mysite.com/index.php?path=var/www/dev.mysite.com/main.php&u=1079
(example url: 1079.dev.mysite.com (where 1079 is a user profile))
RewriteEngine On
# force non-www domain
RewriteBase /
# Parse the subdomain as a variable we can access in PHP, and
# run the main index.php script
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /main.php?u=%1
# Map all requests to the 'path' get variable in index.php
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/].*[^\?])
RewriteRule ^(.*)$ http://dev.mysite.com/index.php?path=%1 [L,QSA]
Now, I wonder:
1) What am I doing wrong?
2) Is what I want even possible?
Much thanks to anubhava for his help in the comments =) as it inspired the solution.
I switched the first rewrite rule to:
RewriteRule ^(.*)$ /main.php&u=%1
Then the last condition to:
RewriteCond %{REQUEST_URI} ^/([^/]*)$
i'm try to do this thing:
I want to redirect all the traffic for a specific domain A to a subfolder of the root. I will do the same thing with multiple domains with their subfolder.
Example:
www.domaina.it -> /public_html/domainadir/
www.unioncucine.it -> /public_html/unioncucine/
For that i have done it using this code that seems to work:
DirectoryIndex index.php index.html
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/unioncucine/
RewriteCond %{HTTP_HOST} ^(www\.)?unioncucine\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /unioncucine/$1
RewriteCond %{HTTP_HOST} ^(www.)?unioncucine.it$
RewriteRule ^(/)?$ /unioncucine/index.html [L]
It seems to work but not in this case:
I want to access www.unioncucine.it/web in which i have loaded a test joomla page (it has not template because i'm working on it).
If you go here: www.unioncucine.it/web i'm redirected to www.unioncucine.it/unioncucine/web/ and it does not load properly contents.
Going here: www.unioncucine.it/web/ it works fine. (The page is just some words and a css scretch).
#Changing
RewriteRule ^(.*)$ /unioncucine/$1
#To
RewriteRule ^(.*)$ /unioncucine/$1/
It seems to work without redirect but all the files inside other folders are not available.
Thank you
Edited:
Changed as you told:
RewriteCond %{REQUEST_URI} !^/web/templates/protostar/ [NC]
RewriteRule ((?:css|js|images)/.+)$ /web/templates/protostar/$1 [L,NC,R=301]
RewriteCond %{REQUEST_URI} !^/unioncucine/
RewriteCond %{HTTP_HOST} ^(www\.)?unioncucine\.
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /unioncucine/$1/
RewriteCond %{HTTP_HOST} ^(www.)?unioncucine.it$
RewriteRule ^(/)?$ /unioncucine/index.html [L]
But it still don't work: http://www.unioncucine.it/css/template.css
You're getting redirected from www.domain.it/domainadir/test to www.domain.it/domainadir/test/ due to mod_dir which adds a trailing slash after a directory.
Add these 2 lines on top of your .htaccess before other existing code:
DirectoryIndex index.html
DirectorySlash Off
Though you should add rule to add trailing slash as well in your rule as:
RewriteEngine On
# fix css/js/images
RewriteCond %{REQUEST_URI} !^/web/templates/protostar/ [NC]
RewriteRule ((?:css|js|images)/.+)$ /web/templates/protostar/$1 [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/domainadir/
RewriteCond %{HTTP_HOST} ^(www\.)?domaina\.
RewriteRule ^(.*)$ /domainadir/$1/ [L]
For css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's header:
<base href="/" />
I created a test.php in my root. and wanted it to be accessible but its giving me a 404 error. Please see below for my .htaccess config.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I tried adding "RewriteCond $1 !^(test.php)" above the rewriteRule but its still not accessible. Can anyone give me an advice on how I can make the test.php accessible in a browser, I want to access it in a browser by going to this URL: www.domain.com/test.php
Thanks in advance!
Try this:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|css|test\.php)
RewriteRule ^(.*)$ ./index.php/$1 [L]
Every request will be sent to your index.php except those mentioned in RewriteCond, i.e., css, images and test.php
Replace your first two lines with the following.
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Also your index.php should be accesible with or without that code so it's not needed.
I'm building a small local social good community website where every project has his own url, such that when username='foo' the URL becomes http://website.com/foo similar to Twitter. Even though I'm not experienced with htaccess I've managed to make it work to a large extent. However I am now running into problems when someone types in http://website.com/subdomain/foo. This now also tries to find the username and display the page, but I don't want that to work and want to show them our 404 page.
Also, on some subdomain's (i.e. about) we have other pages such as 'ourstory.php'. I'd really like to display 'about/ourstory' instead of about/ourstory.php, but only for selected subdomains.
Finally, when having the structure 'website.com/subdomain/subdomain', it generates internal server errors when requiring other php files, even when absolute paths are used.
Here is my current mod_rewrite, I hope you can understand what I try to do and feel free to optimize, I've probably made some beginner mistakes.
This mod rewrite does three things:
1. Remove 'www' infront of address;
2. The second makes sure that when they just type the homepage, it takes them to the homepage(removing this, would result in an empty homepage for some reason, can this be done in another way?);
3. When a name is typed that isn't a subdomain, it redirects them to the profile page and makes the url the username.
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ http://website.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteRule ^(.{5,16})$ profile.php?scrname=$1
RewriteRule ^(.+)(\.php|\.html)/$ /$1$2 [R=301,L]
</IfModule>
Again, any help will be greatly appreciated.
You might find it easier to maintain if you use a front controller instead. Basically, you would redirect all requests that aren't for static files to a single php script, which can then perform advanced rules more easily expressed in code than mod_rewrite rules.
Your .htaccess might then look a little like:
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*)$ index.php?q=$1
In your PHP code you could then process this by examining $_GET['q'] to decide which page to render.
I have made some progress since I posed the question.
Here is the (seemingly very simple) mod_rewrite I ended up with:
<IfModule mod_rewrite.c>
//mod_rewrite by Marc
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./project.php?id=$1
</IfModule>
It does everything I wanted, fixed the internal server errors and therefore I don't need the rewrite to remove the .php anymore. I hope it can help someone else.