Sorry if the title is confusing.
I have set up a virtual host on my local machine. I've set http://dev to map to my /htdocs/dev folder. With some help from dnsmasq and .htaccess, I've set it up so that it maps subdomains of .dev to folders inside /htdocs/dev. And it all works perfect when I try to access, for example, http://dev/file1.html or http://folder.dev/file2.html. The problem occurs with accessing subfolders. I get a 404 Object not found if I try to access http://folder.dev/subfolder/ or http://folder.dev/subfolder/file3.html.
I guess it can be solved with .htaccess, but I failed to do it although I tried. Here's how my /htdocs/dev/.htaccess looks like:
# Default index file
DirectoryIndex index.php
# Interpret .html files as .php scripts
AddHandler php5-script .php .html
# Redirect subdomains to their respective folders
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.dev$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI} [PT,L]
I should mention that, if I try to access http://dev/folder/subfolder/file3.html, there are no problems.
How can I set addresses such as http://folder.dev/subfolder/ point to /htdocs/dev/folder/subfolder?
Try changing your rules to:
RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.dev$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%2{REQUEST_URI} -d
RewriteRule ^ /%2%{REQUEST_URI} [PT,L]
First, thanks goes to #JonLin for giving me a hint about this.
I've managed to solve this with this .htaccess file:
# Ordered list of index files, if none exist, show directory listing
DirectoryIndex index.php index.html
# Interpret .html files as .php scripts
AddHandler php5-script .php .html
# Redirect subdirectories to their respective folders
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.dev$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.dev(.*)?$ [NC]
RewriteRule !^%2\.dev%3?/$ http://dev/%2%{REQUEST_URI}/ [P]
What Rewrite part of this does is it takes a URL in form of http://folder.dev/subfolder and points it to /dev/folder/subfolder while preserving the URL. It works with or without www.
Related
My website is located in a subfolder (gng2) of my public_html folder. The below code from the public_html/.htaccess file works fine in the sense that typing my domain loads the website properly from the subdirectory:
## redirect to subfolder containing the app.
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
My problem is that www.example.com/gng2/whateverpage also works. I want to rewrite/reroute these urls to www.example.com/whateverpage so that the "gng2" subfolder does not show up for example in Google Analytics results.
How should I modify the above code to achieve this?
Thanks,
W.
You can use this redirect rule in gng2/.htaccess to remove /gng2/ from all URLs:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+gng2/(\S*) [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# other rules appear below this line
Please keep your htaccess file inside gng2 folder and try following rules in your htaccess rule file. Please make sure these rules are at top of your file(for applying http/HTTPS to URLs, in case you have more rules), also please do clear browser cache before testing your URLs.
RewriteEngine ON
## redirect to subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
##Place your RewriteRule here.....
I'm using the micro framework Silex on my website hosted on a VPS.
So, the site files are in the /site_name/public_html/ folder but, with Silex, the site must point to the /site_name/public_html/web/ folder.
In the public_html directory, I have the following .htaccess file :
Options -Indexes -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirect to https & www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# Redirect incoming URLs to web folder
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
</IfModule>
And, in the /public_html/web/ folder, the following .htaccess :
<IfModule mod_rewrite.c>
# Redirect incoming URLs to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Now, everything works fine but my pages are accessible with three different patterns :
example.com/page/ (the one I want to keep)
example.com/web/page/
example.com/web/index.php/page/
I have used the meta canonical to avoid duplicate content but I still want these last two options to not exist.
I guess I have something to change in both .htaccess files but I can't find what it is.
I would actually remove the .htaccess file in the /web subdirectory altogether and rewrite directly to /web/index.php in the root .htaccess file. By having two .htaccess files you are seemingly creating extra work. The mod_rewrite directives in the subdirectory will completely override the parent directives (by default), so your canonical HTTPS and www redirects are also being overridden.
(Presumably you had a RewriteEngine On directive in the /web/.htaccess file?)
Having removed the /web/.htaccess file, try something like the following in your root .htaccess file:
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /web
# Redirect to https & www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L,NE]
# If /web or /index.php is present at the start of the requested URL then remove it (via redirect)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(?:web|index\.php)/(.*) /$1 [R=302,L]
# Front-controller...
# Internally rewrite all requests to /web/index.php (uses RewriteBase set above)
RewriteRule index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The check against the REDIRECT_STATUS environment variable ensures we only test initial requests and not requests that have been later rewritten.
The <IfModule> wrapper is not required, unless your site is intended to work without mod_rewrite.
Note that a request like /web/index.php/page/ would result in two redirects. First to /index.php/page then to /page. Since this is an edge case I would consider a double redirect to be acceptable.
UPDATE: I've removed the "directory" check in the above as this would have prevented the document root (example.com/) from being rewritten to the /web subdirectory. This would have consequently resulted in a 403 if you didn't have a directory index document (eg. index.php) in the document root of your site. (However, requests for example.com/page/ should have still worked OK.)
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure it's working OK - to avoid any caching issues in the browser. Be sure to clear the browser cache before testing.
I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.
This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>
When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.
Here is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>
What am I doing wrong?
You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:
In VirtualHost
ServerName example.com
ServerAlias *.example.com
DocumentRoot /path/to/your/doc/root
In htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]
I'm running a Magento site and needed to upload a google site ownership confirmation file to the root. When I did this, html file was not accessible via url when typed as follows www.website.com/google0564e497cc4446t6.html instead I got a 404 page. Other file formats work just fine. I concluded that there must be a .htaccess redirect.
This is the .htaccess content:
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
## RewriteRule ^(.)ItemDesc.asp?IC=(.)$ $1$2 [L]
## RewriteRule ^supply-items/$ ItemDesc.asp?IC= [NC,L]
RewriteRule ^([^/]*)\.html$ /ItemDesc.asp?IC=$1 [L]
##RewriteRule ^ItemDesc.asp?IC=$ supply-items/ [L,R=301]
RewriteRule test_rewrite\.html http://www.website.com/rewrite_successful.html [R=301,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As it turns out the previous website redirects are very important for maintaining of SEO, so I cannot just remove them. However, is there a way to make an exception to specific .html files such as www.website.com/google0564e497cc4446t6.html using the .htaccess file?
Thank you!
You can modify the first part of your rule and add a condition to skip the google file.
RewriteCond %{REQUEST_URI} !^/google(.*)\.html$
RewriteRule ^([^/]*)\.html$ /ItemDesc.asp?IC=$1 [L]
We're setting up a TYPO3 installation, and if the user calls example.com/ we'd like the server to redirect to /typo/index.php?id=106.
This should happen without a change in the address bar. Every other file access on the server (for example example.com/test.png) should be redirected to example.com/typo/test.png).
This is the .htaccess file in the root directory. As I understand, it will redirect everything which doesn't have /typo in the URL to the subfolder and attach the parameters:
RewriteCond %{REQUEST_URI} !^/typo/
RewriteRule ^(.*)$ typo/$1 [L]
Now, this already seems to work, when I call example.com/index.php?id=106 I'm not getting a 404. Unfortunately TYPO3 seems to have some trouble (or the .htaccess configuration isn't correct), because we get a message saying "No input file specified".
What's also missing is the initial redirect when no path is specified. It should then go to /typo/index.php?id=106.
You may try this in one .htaccess file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# URL with no path
RewriteCond %{REQUEST_URI} ^/?$ [NC]
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule .* /typo/index.php?id=106 [NC,L]
# URL with path
RewriteCond %{REQUEST_URI} !^/typo [NC]
RewriteRule ^(.+) /typo/$1 [NC,L]
Maps silently:
http://domain.com/ to
http://domain.com/typo/index.php?id=106
and
http://domain.com/anything
http://domain.com/typo/anything
For permanent redirection, replace [NC,L] with [R=301,NC,L]