I have 2 domain names, foo.com and bar.com and a .htaccess file at the root of bar.com asking all requests to bar.com to redirect to foo.com except the initial search for a directory index file, which gets pointed to a specific php file (functions.php) at foo.com.
This means that a request for bar.com or www.bar.com will see foo.com/functions.php accessed and the various requests for images, stylesheets etc. continue to access the rewritten relative path of foo.com/images/image.jpg rather than foo.com/function.phpimages/image.jpg. This all works fine.
Now, the problem.
I want to include a subdirectory on bar.com that doesn't redirect. As in, a request for bar.com/newdirectory/ goes to that directory and all relative file requests behave as normal while keeping the original rule (described above) for anyone accessing bar.com at root. Currently I get a 404 if I try and access the new subdirectory.
In my original .htaccess file I had included a loose code for checking that files do not exist. I tried adding the flag !-d to check if directories don't exist first but this has resulted in requests to bar.com going straight to foo.com rather than foo.com/functions.php
Current .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Check the request isn't an existing file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ http://www.foo.com/functions.php [P]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteRule ^(.*) http://www.foo.com/$1 [P]
You can add after RewriteBase / :
RewriteRule ^newdirectory/? - [L]
And for others. No need to test file, because you redirect empty URI only.
You can use :
RewriteRule ^$ http://www.foo.com/functions.php [P]
RewriteRule ^(.+) http://www.foo.com/$1 [P]
Which gives for all .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^newdirectory/? - [L]
RewriteRule ^$ http://www.foo.com/functions.php [P]
RewriteRule ^(.+) http://www.foo.com/$1 [P]
Related
I have a directory structure as follows:
.htaccess
api.php
template/index.html
When calling domain.com/api/someendpoint, the request is forwarded to api.php.
# enable apache rewrite engine
RewriteEngine on
# Deliver the folder or file directly if it exists on the server
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-d
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
now, when calling domain.com, the request should be called to the index.html in the template directory:
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
this does not work, the directoryListening is always displayed.
What is the correct way to get the index.html?
RewriteCond %{REQUEST_URI} ^$
RewriteRule . template/index.html [QSA]
The RewriteRule pattern (ie. .) unnecessarily matches everything except the document root. And the REQUEST_URI server variable always starts with a slash, so the condition will never match anyway. So, basically, the rule is doing nothing
To rewrite just the root then you just need a single rule in .htaccess:
RewriteRule ^$ template/index.html [L]
Ideally, this would go before your existing rewrites for your API, although it doesn't strictly matter since the other rewrites do not rewrite the root directory (because of the restrictive conditions).
# Push every request to api.php
RewriteRule ^(.*)$ api.php [QSA]
Should everything really be sent to api.php, or perhaps just URLs that start /api/, as in your example? At the very least, you should be using the + quantifier, not *, since you want to exclude requests for the document root (currently, the 3rd condition is blocking requests to the document root from being rewritten).
Note that if your URLs are of the form /api/someendpoint and you are rewriting to /api.php then you need to ensure that MultiViews is disabled at the top of the .htaccess file:
Options -MultiViews
My current .htaccess is in httpdocs.
How can I configure it to help domain point to subfolder is /vn/
and still keep: www.example.com instead of www.example.com/vn/
Try the following near the top of your .htaccess file in the document root:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /vn/$1 [L]
This internally rewrites all requests that hit the document root to the /vn/ subdirectory. The RewriteCond directive that checks against the REDIRECT_STATUS environment variable ensures the request is only rewritten once - thus avoiding a rewrite loop.
In .htaccess add these lines :
RewriteEngine On
RewriteRule !^blog/ /blog%{REQUEST_URI} [L,R=301]
Or in some hosts :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomainName.com$
RewriteRule ^(/)?$ blog [L]
I have 2 domains using the same webspace, originaldomain.com and newdomain.com
I have placed a .htaccess file at the root of newdomain.com so that if visitors go to it, it shows (but masks) the url of:
originaldomain.com/script.php
I can get something similar using the following rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ http://www.originaldomain.com/script.php [P]
However, style sheets, images and javascript are not loading. I assume one of two things:
They are not applicable to the mod_rewrite rule and are not found
when originaldomain.com/item1.jpg is being searched for at
newdomain.com/item1.jpg
All files may be subject to having a php file stuffed before their
name, so that originadomain.com/item.jpg is now being looked for at
newdomain.com/portfolio.phpimage1.jpg
I'm looking to create a situation where:
If somebody visits newdomain.com/?variables it masks and loads
originaldomain.com/script.php?variables
If somebody visits www.newdomain.com/?variables it masks and loads
originaldomain.com/script.php?variables
All other file requests from the root of newdomain.com are pointed to
relative links at originaldomain.com, so that
http://newdomain.com/css/stylesheet.css is loaded and masked from
http://originaldomain.com/css/stylesheet.css
Any input appreciated, thank you.
From trial and error, this appears to be working at the moment:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ http://www.originaldomain.com/script.php [P]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteRule ^(.*) http://www.originaldomain.com/script/$1 [P]
I have two webspaces, one domain. One webspace contains ~ 5 TB Audiofiles & Images, and one webspace is for the script.
My problem:
Take for e.g. my webspace url are domain1.tld & domain2.tld
On domain2.tld are all(!) files and and other things.
On domain1.tld i uploaded only the php files.
The problem is that if i want to see a video, which is for e.g. on domain2.tld/files/video01.mp4 and the link shows domain1.tld/files/video01.mp4 - how can i change this if this file not exist to go to domain?
I saw here a similar question.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^subdirectory/?$ http://newurl.com [L,R=301,NC]
But this have 1 problem:
If i go to http://domain1.tld/%FILE% and if this not exist -> it should be located to http://domain2.tld/%FILE%
And is it possible to work with mod_rewrite?
So i mean, if domain1.tld/%FILE% dont exist, it should be the same url but it should be the access of domain2.tld/%FILE%
Thanks!
Try that:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ http://domain2.tld/$1 [L,R=302,NC]
It means: if file or directory or symlink not found here, do redirect to domain2.tld .
(It's 302 because testing new rewrite rules with permanent redirect is dangerous )
I am trying to write a .htacess file such that:
The site runs php 5.4
Requests to domain.com run index.php first
Requests to http://domain.com/checkout are redirected to https://domain.com/checkout
All requests to domain.com are redirected to www.domain.com
So here is my attempt:
<IfModule mod_rewrite.c>
# Use PHP 5.4
AddType application/x-httpd-php54 .php
Options -MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/checkout|/order)
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>
But when I make a post to domain.com/cart the user is automatically redirected to domain.com/index.php
Please let me know where I am going wrong...
First off, you need to understand that such rules in .htaccess files are run on a first-come, first-serve basis. Also, you are using the L flag for each of them.
So, you should make sure that the two conditions and rule for silent-mapping to index.php should come last. Move those down to below the https and www-on rules.
Now, of course a request to /cart will be mapped to index.php; that's what it's programmed to do. However, you say "redirected"... Does that mean that it shows index.php in the address bar? If that is the case, the fix I've mentioned should sort that out (always check for redirects first, and then do the necessary mapping).
Like #scotsninja said in the comments, these things should be handled by Laravel itself. Your .htaccess file should only be used to map anything that is not a file or directory to the index file, or Laravel bootstrap.