.htaccess mapping www & non-www to folder and maintain www - php

I am no .htaccess expert and extremely rarely use it. I went through several solutions on Net and SO but no luck still. So I have one IP says 1.2.3.4 that has sub folder of my wordpress application says ABC in the /var/www/html/. DNS has been setup so that www.mydomain.com and mydomain.com point to IP 1.2.3.4.
I have my content in .htaccess file as below and it only supports mydomain.com. The content is displayed as expected except when I put www.mydomain.com it displays Linux home page.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteCond %{REQUEST_URI} !^/ABC/
RewriteRule (.*) /ABC/$1
When I put www.mydomain.com the browser automatically change the URL to mydomain.com.
Apart from that, I would also like to have all domain with www. Means request from mydomain.com will be displayed as www.mydomain.com. Later I will just put www.mydomain.com in the URL config of my wordpress. Some helps are appreciated.

Switch back to your old .htaccess code. I got it more clear after your last update. You have wordpress files in "public_html/ABC" but need your website url as www.example.com. Let me explain step by step.
Paste your old code in .htaccess.
copy (do not move) the index.php and .htaccess file from the /ABC folder to the "public_html" of your site.
Find one rule in "public_html/index.php" as "require( dirname( FILE ) . '/wp-blog-header.php' );" and replace it with "require( dirname( FILE ) . '/ABC/wp-blog-header.php' );" (do not edit the index.php file in the folder /ABC!!)
change ONLY the "Site Address (URL)" in the general settings (Settings -> General) to "http://www.example.com".

So finally I managed to get what I want to be working with below solution. Hopefully this will help somebody out there:
RewriteEngine on
RewriteBase /
# Whatever request coming to the server, add www to it
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Map the www URL to the subfolder in root directory
RewriteCond %{HTTP_HOST} www\.myurl\.com$
RewriteCond %{REQUEST_URI} !^/thefolder/
RewriteRule (.*) /thefolder/$1
For Wordpress part, I have put codes in wp-config.php as below:
define('WP_HOME','http://www.myurl.com');
define('WP_SITEURL','http://www.myurl.com');
Thank you. Cheers.
UPDATE
I have just realized that after the changes above, I could not able to login to my WP admin page. This is due to wrong redirect. It looks something like http://www.myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.myurl.com%2Fthefolder%2Fwp-admin%2F&reauth=1
When I remove the thefolder from URL it redirects to expected page. Can somebody advice on this?

Related

Multisite Wordpress, SSL and htaccess to use the subfolder as a root

I have an SSL sertificate, so http -> https is a must (as a precaution). I intend to have multiple subdomains, i.e. subdomain1.example.com, subdomain2.example.com, currently there is one subdomain that works without any issues. I'm using a multisite Wordpress setup, that was installed (purposely) in one subfolder. The multisite setup is for other languages. The current server folder layout is as follows:
public_html
backstage
subdomain1 (folder for the subdomain)
frontstage
wp-admin
wp-content
wp-includes
(the rest of the WP files)
index.php (a test file, that shouldn't load if the redirection is set up properly)
Currently, the www.example.com/frontstage/ opens the main WP site, this is fine. I can access its wp-admin without issues. www.example.com/frontstage/en/ shows a 404 page, this is not fine. www.example.com/frontstage/en/wp-admin/ opens the dashboard fine for the other site.
I want to retain the stripping out of index.php from any links (to keep the links clean).
There are two "simple" things to configure properly:
I want to retain the server folder structure as it is, but having the "frontstage" folder skipped, so that when you visit www.example.com, the main WP site loads (and in the case someone would load www.example.com/frontstage/ it would redirect to www.example.com). Naturally, the "shift" needs to allow for the www.example.com/en/ to open the secondary website (any any other language sites that may follow). Ideally without rewriting all the links within the WP sites.
Currently the /en/ site doesn't load its root. The demo posts and pages load fine.
My current .htaccess on the root level looks like this:
# disable index.php from urls
RewriteEngine On
RewriteBase /
<IfModule mod_rewrite.c>
# redirect index.php requests
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
# force https and www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# move wordpress one level up
# allow subdomain
RewriteRule ^backstage\/subdomain1\/?(.*)$ "https\:\/\/subdomain1\.example\.com\/$1" [R=301,L]
Any help with this is highly appreciated (I'm still learning the htaccess bits and tricks and this one is truly beyond me). What am I missing in the above code to get it right?
When I have WP installed within a subfolder, I just goto Settings => General and change the Website URL by removing the subfolder.
Afterwards I move the file index.php one folder level up and in your case, would change it to:
require( dirname( __FILE__ ) . '/frontstage/wp-blog-header.php' );
Does this solve your problem?
Find a detailed description how to move WP to a subdirectory on this site - You can find a detailed description about putting Wordpress in a subdirectory on the following website https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
Regarding a redirection from http to https - this is possible within the .htaccess. Much better, in my opinion, would be an alias on the apache (this usually should be done by your hoster).
The difference:
The htaccess redirect goes back to the user and then again to the server, which costs some time. The alias is - as far as I know - redirected on the server.
if you want to force https, you can do it with the following entry.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Forcing www to a sub domain in .htaccess cause 500 internal server error

Suppose My domain is
example.com pointed to the server root (public_html)
I have created a sub domain
dev.example.com Pointed to the directory (public_html/development)
I wanted to always open my both site (main + subdomain) with www prefix
for that I place the below code to .htaccess
RewriteCond %{HTTP_HOST} !^www\.
#RewriteCond %{HTTP_HOST} !^dev\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Main site working fine its redirect domain.com to https://www.example.com
But dev.example.com redirects to https://www.dev.example.com/development and cause 500 Internal Server Error.
It should be redirected to https://www.dev.example.com instead of https://www.dev.example.com/development
Where I am going wrong.
I tried different Codes in .htaccess but nothiing works.
Converting my comment into an answer to that it might help someone facing similar problem.
Move above rule in development/.htaccess so that per directory directive RewriteRule sets $1 value relative to development directory instead of public_html directory.
Make sure to test it after clearing browser cache.

htaccess mod_rewrite redirect to directory without displaying on url

I have a site called www.example.com and I have my php files in it. I store all my working files in www.example.com/site. I want to view the site in www.example.com instead, without moving my site content. What can I do?
This is currently what I am typing in .htaccess. It will redirect my site to www.example.com/site but I think the url is ugly
RewriteEngine on
RewriteBase /
RewriteRule ^(/.*|)$ /magento$1 [NC,L]
Let me see if i understood this correctly.
You have a domanin, www.example.com, and on this domain you want to display the content of a directory, www.example.com/site !?
If this is the case then you need to change the document root
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]
or
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /site/$1 [L,NC]
There are 2 things you must consider :
If I understand your request, you are accessing some PHP files using www.example.com, but what you want is to access www.example.com/site, but without the /site, right ?
So basicaly, what you're looking for is NOT rewrite, it's just pointing your domain to the good folder, which is /site, right ?
If you're using Apache2, you have to edit your apache's configuration file in /etc/apache2/site-available/default (or remplace default with the name of the virtualhost you may have created).
In this file, look for the directive DocumentRoot. It should lead to the "root" directory of your pages (the one you access typing www.example.com)
I think you just have to append /site to this DocumentRoot and then reload your apache2 with service apache2 reload
You're website www.example.com will now lead to the correct directory.
If it's still not working, you must consider looking into magento's admin, because magento is rewriting url according to the Base URL you specify inside Admin / Configuration / general / web.
You'll have to modify Base_URL in both Secure and Un-Secure sections.
Then it should work fine.
I would comment out the Rewrite bloc you're using at the moment, or maybe I didn't fully undestand what you want to achieve.

htaccess redirect root to subdirectory but allow index.php in root to function

This is our situation: The original domain name (say, www.example.com) was done in WordPress and installed in the root. We have created a new website using Joomla and placed it in a sub-directory (say, www.example.com/PORTALsite).
I have been able to successfully redirect the root (www.example.com/) to this sub-directory (www.example.com/PORTALsite) using this code in htaccess:
RewriteEngine on
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
This works good, EXCEPT that we need the original WordPress website (the Home page located at www.example.com/index.php) to still function. We need this, because we have hundreds of pages within this WordPress site that are indexed in the Search Engines and we want all of that old content to continue to function.
The code above works well for all files and sub-directories within the Root, allowing our old content to continue to function, and it correctly redirects the root itself (www.example.com/) to the new Joomla website's sub-directory (www.example.com/PORTALsite).
HOWEVER, the problem is this: We still need the old WordPress site's index.php file to function if it is accessed directly (i.e., if someone types in www.example.com/index.php), BUT it is NOT functioning. If you go to www.example.com/index.php is also redirects to www.example.com/PORTALsite. Though all of the other files in the root still function as we desired them to.
Let me make sure I am explaining this properly: If someone goes our domain (www.example.com) it redirects as we want to the new Joomla website (at www.example.com/PORTALsite). BUT, if you go to www.example.com/index.php (the old WordPress site's Home page) it also redirects to the new Joomla website's sub-directory (www.example.com/PORTALsite). We do Not want this to happen. We want it so that if the root's index.php file is accessed directly, that it will still function (to keep all the old WordPress content intact).
We ONLY want requests to the domain name itself, www.example.com, to redirect to www.example.com/PORTALsite. But, we need all of the other files and sub-directories, including www.example.com/index.php, to still function (so that people can still access all of the old WordPress website's content, as well as numerous static pages).
All the files and sub-directories in the root (www.example.com/) still function as we want, BUT just the old WordPress site's Home page (located at www.example.com/index.php) also redirects to the new site at www.example.com/PORTALsite (along with requests for the domain name itself www.example.com). So the code above is VERY close to working as we desire.
I greatly appreciate the expertise here on StackOverflow and greatly appreciate any help we can receive. Thank you in advance!
UPDATE: This is the code in our HTACCESS file: (used "example.com" rather than true domain name)
RewriteEngine on
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
In light of anubhava responses below, his code should work once I get WordPress to stop removing the "index.php" from the URL. Thank you anubhava! Here's the whole file.
Replace this rule:
RewriteRule ^$ http://www.example.com/PORTALsite [R=301,L]
with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+[\s?]
RewriteRule ^$ /PORTALsite [R=301,L]
Make sure this is first rule in your wordpress .htaccess
Make sure to test in a different browser to avoid 301 caching issues
Change permalink setting of WP to make your BLOG/Home URLs as http://domain.com/index.php instead of http://domain.com/

Understanding subdomain url mod-rewrite

I need to understand url mod-rewrite but couldn't get the basic at all.
I want to change www.domain.com/folder/page.php?id=1 to 1.domain.com
I created a wildcard subdomain (*.domain.com) and the problem is, where should I point the subdomain to? is it public_html/ or public_html/folder/ ?
Which htaccess file should I modify? is it in public_html/ or public_html/folder/ ?
What's the link to the page.php? is it a href="1.domain.com" or a href="www.domain.com/folder/page.php?id=1"
Thank you very much :D really appreciate your help :D
EDIT :
.htaccess code :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /file.php?item=%2 [QSA,nc]
I typed 1.domain.com but it's showing no server found.
That strongly depends on your setup. Point it to the same directory where you'll put the .htaccess in. Most of the time a subdomain points to an extra directory i.e. public_html/1/ where you can put the .htaccess in and separate it's logic from the rest of your website.
That one where the subdomain points to, see answer 1. :)
The link will be a href="1.domain.com"

Categories