Wildcard subdomain redirecting to a file - php

I'm trying to redirect foo.bar.com to bar.com/foo.php, but I have no idea where to start and everything else has failed me so far. I've tried many things here on StackOverflow, but none of them seem to be working at all (just various Apache errors).
I already have a wildcard subdomain set up from cPanel, but I'm not really sure where to go from here. Is .htaccess even the best option for this?
EDIT: I also want to rewrite the URL from bar.com/foo.php to foo.bar.com.

You can use this generic rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ http://%2/%1.php [L,R=302]
EDIT: To hide the rewritten URL (internal rewrite) use this:
RewriteCond %{HTTP_HOST} !^(?:www\.)?domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(.+)$ [NC]
RewriteRule ^/?$ %1.php [L]

Related

While redirecting all pages with .htaccess my codes are conflicting

I am trying to redirect to
https://example.com from https://www.example.com, http://example.com, http://www.example.com
------------------ and ------------------
https://example.com/sub_page/pages from
https://www.example.com/sub_page/pages, http://example.com/sub_page/pages,
http://www.example.com/sub_page/pages
on .htaccess but my sub_pages are not redirecting properly. probably because of conflicting codes below. I read many articles on Internet but do not fount useful.
my .htaccess file
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example.com$
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think you got my whole point. In short, I want to redirect all requests to non=www and https. Please help me with this,
This probably is what you are looking for:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
That should work in the actual http server configuration or, if you have no access to that, in a distributed configuration file (".htaccess) located in the http hosts's document root.

HTTP to HTTPS redirect issue .htaccess

I'm a newbie to coding and I'm using following codes in my .htaccess.
My issue is my site is not redirecting from http://example.com to https://example.com with this code. Did research but couldn't find something that goes with my code.
I do not know if this is the correct order, or the most efficient way to achieve this. Any input would be greatly appreciated!
Remove WWW, from both HTTP and HTTPS an force HTTPS
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ https://%3%{REQUEST_URI} [R=301,L]
Hide .php, and redirect .php files to new non-extension URLS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
Give this a go and let us know if it works out for you. looking at your .htaccess for your SSL looks like it has way more in it then there needs to be. This is what I use and seems to work fine
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This will redirect all traffic to an https://

Conditional .htaccess for multisite

I am having a minor trouble in .htaccess file, I have looked into some stack links, but couldn't resolve my issue.
I have hosted a multi domain site, what I want is: If there is a subdomain and I access this url:
subdomain1.example.com/sitemap.xml
subdomain2.example.com/sitemap.xml
This should pick up the sitemap.xml file from subdomains/subdomain1 and subdomains/subdomain2 respectively, which works great with this rule:
RewriteCond %{HTTP_HOST} ^(.*)\.example(.*).com [NC]
RewriteRule ^(sitemap.xml|robots.txt)$ subdomains/%1/$1 [L,NC]
If there is www.example.com then pickup the sitemap.xml from project root folder.
I have tried this, but not working:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(sitemap.xml|robots.txt)$ http://www.example.com/$1 [L,NC]
It gives me 500 internal server error.
Your RegEx condition in RewriteCond %{HTTP_HOST} ^(.*)\.example(.*).com [NC] matches even www.example.com, so there will be a redirect loop.
The rewrite condition alternative that you specified as RewriteCond %{HTTP_HOST} ^www.example.com [NC] won't work as it is does not match www.example.com
Try this Regex condition
^(?!www).*\.example(.*).com
OR
RewriteCond %{HTTP_HOST} ^(?!www).*\.example(.*).com [NC]

htaccess, redirect to https for a specific file and to non-www https

I've a site to work and trying to make redirect by htaccess for few days. I've searched the net and found good works, especially in this site, but altough I've tried almost every possibilities, I've could not achieve what I want to do.
My need is redirect all site to non-www http, including https, except for only one file. Let's say redirect
http ://www.example.com/.../....php?a=...
https ://www.example.com/.../....php?a=...
https ://example.com/.../....php?a=...
to
http ://example.com/.../....php?a=...
However, only one specific file
http ://www.example.com/.../theSpecificFile.php?a=...
https ://www.example.com/.../theSpecificFile.php?a=...
http ://example.com/.../theSpecificFile.php?a=...
should be redirected to
https ://example.com/.../theSpecificFile.php?a=...
To do these, I've wrote many htaccess files but in each case, I couldn't achieve my needs. e.g. At the last htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 http://example.com/404.php
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(theSpecificFile\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
#redirect www.example.com to example.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
By this htaccess file,
when I try to https ://www I get Unsecure or Untrusted connection (I'm translating from another language, hope it might be true translation) with ssl_error_bad_cert_domainand when I try to access to the theSpecificFile.php I get error defining "infine loop" (again I hope this might be a true translation).
This is really frustrating for me, so, any help would be highly appreciated.
Thanks in advance
Look, here you redirect theSpecificFile.php to its https version, but then the second redirect rule triggers and redirects the browser back to http, and from there the first rule redirects back to https... That's why you're getting the infinite loop.
And ssl_error_bad_cert_domain means the certificate isn't for this domain, so you might need to check if you're using the right one.
You can use that:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{REQUEST_URI} !/theSpecificFile\.php [NC]
RewriteRule ^ http://example.com/%{REQUEST_URI} [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /theSpecificFile\.php [NC]
RewriteRule ^ https://example.com/%{REQUEST_URI} [L,R=302]
Change [R=302,L] to [R=301,L] when everything works well
Please try:
RewriteCond %{HTTPS} on
RewriteConf %{REQUEST_URI} !=/theSpecificFile.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

htaccess: redirecting site domain to subdomain

I need to redirect users accessing mysite.com to www.mysite.com in my .htaccess file.
The problem is anything I try won't work, I think because ^mysite.com doesn't exclude www.mysite.com so that will create a bad loop, but I usually get error 500.
I also need to exclude another subdomain, the img.mysite.com.
How can I do that?
I used many examples from the net & a few htaccess generators, which gave me this code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com[nc]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [r=301,nc]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is what you need - taken from the default Drupal .htaccess
Try something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteRule ^/(.)$ http://www.mywebsite.com/$1 [L,R=301]

Categories