Redirecting url to access subdirectory - wordpress

I have a wordpress subdirectory install in a directory called 'wordpress'. So currently I am able to access the live site as follows: www.domain.com/wordpress.
My objective is for users to simply navigate to: www.domain.com, omitting the wordpress directory from the url.
I was wondering if someone can help me configure my .htaccess so every time a user navigates to anything with domain.com/wordpress it'll redirect to domain.com.
So for example: domain.com/wordpress/admin will become domain.com/admin
I created a new .htaccess file and placed in the root with the following contents, which did not work :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ wordpress [L]

To rewrite / to /wordpress you can use this rule :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteCond %{REQUEST_URI} !^/wordpress
RewriteRule ^(.*)$ /wordpress/$1 [L]
This will internally redirect all requests from root to the subfolder.
Do not remove the RewriteCond directive otherwise the the rewrite destination will rewrite back to itself causing an infinite loop error.

Related

.htaccess load different web app depending on route under HTTPS

I'm working on a project made by a software engineer and by a team of non-technical people who know how to use Wordpress. That means that part of the platform is handmade, but still need to have Wordpress contents to be handled by non-software engineers.
My idea is to have two folders in my webserver root, one called /app/ containing the handmade code, and one called /wp/. So when the GitHub pipeline release new code into /app/ is sure not to touch stuff in /wp/ containing Wordpress.
I have achieved forcing the HTTPS with the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It works like a charm, users are successfully redirected if they use HTTP.
Now the problem is, I want to "reserve" the routes used by the handmade platform for myself, and in case the user is not calling any of those routes, then I pass the ball to Wordpress. I want it to appear as it is a single website, so I don't want the user to load the /app/appRoute or the /wp/wpRoute, I'd like to always load /route1, /route2, without specifying the subfolder into the URL.
The handmade platform should have priority, and it uses around 13 main routes (and some of them have subroutes), so I can hardcode them into the .htaccess file I guess. If the user is trying to load any of those routes, then I want to load the content into /app/route, if not, I'd load /wp/route. Of course Wordpress has its own .htaccess file, and the platform has its own, which is:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I'm very bad at working on .htaccess files and been searching around, it looks like I can't find the solution for my scenario.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
Should I have two .htaccess, write the 13 routes first, and then eventually redirect to the WP .htaccess (is it even possible?)?
Do I risk to make the user face a "Too many redirects" error?
This hybrid solution confuses me a lot. Does anyone who has been in the same situation has suggestions? Thank you in advance.
Given the following requirements:
/app subdirectory contains the "handmade code"
/wp subdirectory contains the WordPress site.
Neither /app or /wp should appear in the visible URL.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
You could, but I wouldn't. Keep the WordPress .htaccess file in the /wp subdirectory. Everything WordPress is in the /wp subdirectory.
I would use 3 .htaccess files:
One in the document root. This manages the routing to either the "handmade code" in /app or /wp (WordPress). This should also manage the canonical redirects (ie. HTTP to HTTPS and www vs non-www)
One in the /app subdirectory that manages the routing within your "handmade code".
One in the /wp subdirectory that manages the routing within WordPress.
This allows you to keep the "handmade code" and WordPress entirely separate (in terms of development).
Your 3 .htaccess files would then look like this:
/.htaccess
# /.htaccess
RewriteEngine On
# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite specific URLs to "/app" (handmade code)
RewriteRule ^app-route-1$ app/$0 [L]
RewriteRule ^app-route-2$ app/$0 [L]
etc.
# Rewrite everything else to WordPress
RewriteRule (.*) wp/$1 [L]
The "specific rewrites to /app" can be combined if there is a pattern. See below regarding static assets.
/app/.htaccess
# /app/.htaccess
RewriteEngine On
# Redirect any direct requests to "/app" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# Front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
/wp/.htaccess
# /wp/.htaccess
# Redirect any direct requests to "/wp" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Note the RewriteBase directives and slash prefix on the substitution strings are specifically excluded, to avoid having to specify /app or /wp in the .htaccess file itself. (Although this might mess with WordPress, that likes to (unnecessarily) use RewriteBase and will try to overwrite the WP code block.)
You do not need to repeat the RewriteEngine directive, that already occurs later in the WP code block.
I don't know how you want to handle your static assets/resources (CSS, JS, images, etc.)? Currently, the above assumes that you will link directly to the assets within /app, ie. By including the /app path segment in the asset link. eg. <image src="/app/assets/images/myimage.png">. With WordPress you could link directly (ie. include /wp prefix) or omit /wp, since everything else is rewritten to /wp anyway.
Ideally, it would probably be preferable to omit both /app and /wp from your asset links, since you don't want to unnecessarily expose these to your users and it would otherwise make the sites dependent on these parent directories.
If your "handmade code" uses /assets for all the assets then you can rewrite these in the parent .htaccess file in the root, before your custom route rewrites:
# Rewrite "/app" assets
RewriteRule ^(assets)(?:/(.*)|$) app/$1/$2 [L]
This allows your "handmade code" to refer to assets using root-relative URLs, as if the app was installed in the document root.

htaccess redirect all links except base url

I want to redirect all links from my site except the home url to subdirectory.
Example:
www.example.com should remain www.example.com
www.example.com/someurl should redirect to www.example.com/sub/someurl
www.example.com/someurl/anothermore should redirect to www.example.com/sub/someurl/anothermore
Edit: I am currently working on a subdirectory so currently its www.example.com/main/someurl to redirect to www.example.com/main/sub/someurl
To redirect everything to the /sub subdirectory, except the root then you can do something like the following at the top of your .htaccess file in the document root using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sub/
RewriteRule !^(index\.php)?$ /sub%{REQUEST_URI} [R,L]
The above states that if the request is not / (or /index.php) and does not start /sub/ (the RewriteCond directive) then redirect to /sub/<suburl>.
Edit: I am currently working on a subdomain so currently its www.example.com/main/someurl to redirect to www.example.com/main/sub/someurl
That's a subdirectory, not a "subdomain". (?)
If this is the case then move these directives to a /main/.htaccess file and change them to read:
RewriteCond %{REQUEST_URI} !^/main/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/main/sub/
RewriteRule (.*) /main/sub/$1 [R,L]

htaccess redirect -redirect all pages from root to /blog/ folder

Here is what I'm trying to do:
Redirect all pages in ROOT (ex: ROOT/page-name/) directory only to /blog/page-name/ directory
DO NOT redirect sub directories with NAMES - "/cities/", "/states/", "/categories/" under ROOT directory
I just want to redirect all existing Wordpress pages from ROOT to /blog/ and NOT redirect pages that I will be adding later with /cities/, /states/ and /categories/ names in them.
Please help! thanks!
Add this rule to the htaccess file in your document root, preferably above any rules you may already have there:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(blog|cities|states|categories)
RewriteRule ^(.*)$ /blog/$1 [L,R=301]
You should also be able to do something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/ROOT/)([/]?.*)$ [NC]
RewriteRule ROOT /blog/%2 [L,R=301]

.htaccess file: redirect files inside of folder

I am trying to make my server look neat locally (I swear I am OCD) and what I am doing is combining a wordpress blog with all of my subdomains.
My wordpress blog is my main website, example.com. Whereas my subdomains are at sub.example.com.
Right now, I have this setup:
public_html
( wordpress files here )
subdomains
sub1 - (sub1.example.com)
sub2 - (sub2.example.com)
sub3 - (sub3.example.com)
blog - (empty)
The blog folder is empty. However, I want to move all the wordpress files to blog/ but keep the blog's URL at http://www.example.com/. Thus, http://www.example.com/index.php will go to http://www.example.com/blog/index.php, as well as all directories in blog/.
This code did not work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ blog/$1.php [L]
It instead gave me Index of /. Is there any possible way to do this?
The best way to do this would be to just move all the files to /blog and then redefine your Apache siteroot for the www. domain to include the blog folder like
DocumentRoot /(path)/public_html -> DocumentRoot /(path)/public_html/blog
That way you won't even have to use .htaccess rewrites to correctly map your blog folder to the root of the www domain.
If for some reason you can't do that, lets take a look at your htaccess rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
These 2 conflict. Without an [OR] at the end of your RewriteCond, the rules act as AND operator meaning each condition must be true. So you will never had a match that is both NOT a file (!-f) and IS a .php file (.php -f).
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ blog/$1.php [L]
Here you are appending .php to a match that already contains .php per the RewriteCond.
I would try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [NC,L]
If you move all files into /public_html/blog and there will be nothing in /public_html except your /blog and /subdomain folders, you really don't even need !-d & !-f since there would be no files or folders to match (except your subdomain folders but I'm not sure you want those being served up under www domain anyways). Seeing as how your wordpress css,js and images are now also going to be in /blog you would want their urls to be forwarded on too. Also you don't really need RewriteBase / when working in the root folder. Just start your target urls with a slash like /blog/$1. Finally I added in a condition to check that the target request doesn't already contain the /blog in order to prevent infinite redirects...
Don't forget that even with the redirect in there, you will still need to have your site address configured with /blog -
http://www.example.com/blog/
in the Wordpress settings.
Here is another working example:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^$ /blog/$1 [R=303,L]

mod_rewrite http_host

How can I prevent access to a subdirectory based on HTTP_HOST.
I have 2 parked domains pointing to one directory. They both serve up the same website.
However I wish each to have a seperate wordpress blog. To do this I'm installing them in different directories.
domain1.com/domain1-blog/
domain2.com/domain2-blog/
The trouble is the directories are accessible across domains. For instance
domain1.com/domain2-blog/
domain2.com/domain1-blog/
I REALLY don't want people to be able to access across domains. Wordpress will break and I will get duplicate content issues in Google.
What is the EXACT code I need to put into my htaccess file to serve up a 404 error should anyone try to access
domain1.com/domain2-blog/
OR
domain2.com/domain1-blog/
I thought maybe using HTTP_HOST combined with mod_rewrite somehow?
I'm a big newb when it comes to htaccess, so the more explicit you can be the better.
Thanks
Try adding the following to your htaccess file in the root directory of your domain
RewriteEngine on
RewriteBase /
#if its domain1
RewriteCond %{HTTP_HOST} domain1\.com$ [NC]
#and the request is for domain2 blog
RewriteCond %{REQUEST_URI} ^/domain2-blog/ [NC]
#send a 403 forbidden
RewriteRule . - [F,L]
#if its domain2
RewriteCond %{HTTP_HOST} domain2\.com$ [NC]
#and the request is for domain1 blog
RewriteCond %{REQUEST_URI} ^/domain1-blog/ [NC]
#send a 403 forbidden
RewriteRule . - [F,L]

Resources