redirect to subfolder - apache

I have set up url redirect on my host--but its not working.
my htaccess looks like this now
rewriteengine on
rewritecond %{HTTP_HOST} ^.*$
rewriterule ^http:\/\/www\.bangkoksoftball\.info "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L] #4d3a8d4534e567
what i want is any request to http://www.bangkoksoftball.info to be redirected to http://www.bangkoksoftball.info/wordpress/
but any request to a path file or directory off the root not to be redirected
this so people can still access the old site via /home.html or index.html etc.. and still be able to navigate outside the wordpress folder

Try this in your .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^(www\.)?bangkoksoftball\.info$ [NC]
RewriteRule ^/?$ /wordpress/ [R,L,NE]
Remember there is NO presence of http/https in %{HTTP_HOST} or %{REQUEST_URI} variables. Also . needs to be escaped in domain match. NE flag is for not escaping the query parameters.

The rewriterule line does not look at the domain, you must specify the domain the rule applies to in the RewriteCond line. In the rewriterule line, you specify first the paths that should trigger the rule, and the path the user should be sent to instead.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]
This will send only requests to www.bangkoksoftball.info/ to the wordpress folder. If you want to also redirect www.bangkoksoftball.info/index.php to the wordpress folder, you would need to add an additional set of directives:
RewriteCond %{HTTP_HOST} ^http:\/\/www\.bangkoksoftball\.info$
RewriteRule ^/index\.php$ "http\:\/\/www\.bangkoksoftball\.info\/wordpress" [R=301,L]

This should work for you as it worked here:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?bangkoksoftball.info$ [NC]
RewriteRule ^/$ http://www.bangkoksoftball.info/wordpress/$1 [R=301,L]

Related

.htaccess rewrite in subdirectory

I am currently deploying a number of sites from one hosting account. I have all of the sites in their own folder including the primary domain. The issue I have is when I rewrite the primary domains address with my current code, it includes the subdirectory in it. So currently if I type in http://www.example.com/url it rewrites to https://example.com/folder/url. I just want it to rewrite without the folder.
Any ideas. I know I am complicating this by running my primary domain in a subdirectory, just trying to clean up hosting as best as possible.
In my public_html .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder/index.php [L]
and in public_html/folder .htaccess:
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So currently if I type in http://www.site.whatever/url it rewrites to https://site.whatever/folder/url.
This is a "redirect", not a rewrite.
This is happening because of the use of the REQUEST_URI server variable in your HTTP to HTTPS redirect in your public_html/folder .htaccess file:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
The REQUEST_URI server variable contains the full URL-path of the request, which, by the time the subdirectory's .htaccess file is called, has been updated to contain /folder.
You need to either:
Move your canonical www to non-www and HTTP to HTTPS redirects to the .htaccess file in the document root. (This would be preferable if you have no other mod_rewrite directives in your public_html/folder .htaccess file.)
OR,
Modify the above directive to use the $1 backreference (to the captured RewriteRule pattern) as you are doing in the preceding www to non-www redirect. For example:
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
(Note that this should ultimately be a 301 redirect, once you have confirmed it works OK.)
And don't forget to escape literal dots in the regex.

Should be a simple 301 redirect

What I think should be simple is just not working.
I have switched domains
Old URL example:
digital.photorecommendations.com/recs/2015/01/big-zoom-field-review/
New URL example:
photorec.tv/2015/01/big-zoom-field-review/
Really just switching domain and dropping the recs folder from the URL
Using http://htaccess.madewithlove.be/ to test and the outputs the correct URL
Options +FollowSymlinks
RewriteEngine on
RewriteBase /recs
RewriteCond %{HTTP_HOST} !^www\.digital.photorecommendations\.com$ [NC]
RewriteRule ^recs(.*) http://photorec.tv/$1 [L,R=301]
When I place this in the htaccess file I get 404 errors on all the pages except the home page. The htaccess file is inside the /recs folder. I have also tried it in the root directory of digital.photorecommendations.com and I get no results at all.
Any suggestions?
Thanks!
You have wrongly used negation in RewriteCond and regex also needs a fix. Use this rule in /recs/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(index\.php)?$ http://photorec.tv/ [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?digital\.photorecommendations\.com$ [NC]
RewriteRule ^(.+)$ http://photorec.tv/recs/$1 [L,R=301,NC,NE]

.htaccess RewriteRule not retaining capture group during domain redirection

I have an HTTPS subdomain, https://donate.example.com/, which points to the same web root as the www domain. I have a script on my server in the folder /donate/, and inside of there I have an .htaccess file which has the following code. I want to make sure any requests to http://www.example.com/donation/ (or http://direct.example.com/donation/) will redirect to https://donate.example.com/donation/.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^donate\.example\.com$ [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://donate.example.com/$1 [R=301,L]
</IfModule>
The issue I'm having is that the capture-group in RewriteRule (^(.*)$) is not being copied into the destination URL ($1), so the user is only being redirected to the webroot instead of the full path. This means that the user is being redirected to https://donate.example.com/ instead of https://donate.example.com/donation/ (which is the behavior I need/expect).
Any idea why the capture-group isn't working?
Problem is that inside /donation/ folder captured value for http://www.example.com/donation/ will be empty since it is relative to /donation/.
Try this code instead:
RewriteEngine on
RewriteBase /donation/
RewriteCond %{HTTP_HOST} !^donate\.example\.com$ [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://donate.example.com%{REQUEST_URI} [R=301,L,NE]

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess rewrite to simultaneously change domain and remove path

My URL structure is currently as follows:
http://domain.com/folder/filename (CURRENT)
I want to change this so that I can use the following URL instead:
http://sub.domain.com/filename (NEW)
So accessing the CURRENT or the NEW url, should load the file located at the CURRENT url, but show the NEW url in the address bar. It should only apply to the "/folder/" path.
sub.domain.com is a mirror of domain.com, ie. they share the same file system and root directory.
This is what I have so far:
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/folder/?(.*)$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
This is working, but is missing the rule to remove the "/folder/" from the path. I've tried combining multiple RewriteRule's with no luck. Any ideas? Thanks.
UPDATE: Thanks again #Gerben - I understand what your rules are doing now, but the second one isn't working for me. I suspect because it's conflicting with some other rewrite rules, in particular those of WordPress, which are lower down in my .htaccess file:
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
Because of this the page ends up in a redirect loop, ie (from Chrome):
"The webpage at http://sub.domain.com/folder/index.php has resulted in too many redirects." - while the url I was originally trying to access was, for example, http://sub.domain.com/page
Any ideas?
Try:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^(folder/)?(.*)$ http://sub.domain.com/$2 [R=301,L]
This will redirect everything to sub.domain.com, and remove the /folder part of the URI if it is there. If not, it redirects and leaves the URI untouched.
RewriteCond %{THE_REQUEST} /folder/
RewriteRule ^folder/(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/folder/
RewriteRule ^(.*)$ folder/$1 [L]
# WordPress rules here
edit the second R=301 should not have been there
But this won't work, as wordpress has no way of knowing you want folder. You could add the Proxy flag to the rewrite, but then you need to change the rule above to not redirect on this internal proxy request.