.htaccess - hiding subfolder1 in URL but showing subfolder2 (other website) - .htaccess

I have a wordpress website physically located in the "wordpress" subfolder of the root folder of the website. I manage to hide the subfolder "wordpress" in the URL with the following code:
.htaccess on root folder
RewriteEngine On
RewriteBase /
RewriteRule ^$ wordpress/ [L]
RewriteRule ^(.*)$ wordpress/$1 [L]
.htaccess in wordpress subfolder
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
However, I have another website in another subfolder let's call it "other-wp" and this needs to remain as it is, with the URL pointing to:
https://mywebsite.com/other-wp/
Since I managed to hide the "wordpress" folder in the URL, I am unable to acces my "other-wp" it says the page doesn't exist.
I'm not skilled with coding for .htaccess so i don't know what i need to do to fix it.
Could you help?

You need to implement an exception for that second resource:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/other-wp
RewriteRule ^/?(.*)$ /wordpress/$1 [L]
I also made some other modifications to that top level configuration file, just smaller optimizations. In general you should check if you can place such global rules in the actual http server's host configuration. Using distributed configuration files (".htaccess") is just a fallback if you have no access to the real configuration. They work, but come with disadvantages.

Related

Yii2 app in a subdirectory of a Wordpress website: cannot enable pretty urls

I’m trying to deploy a basic webapp on a shared environment where Wordpress is on the root. The Yii2 app is in /subfolder.
I’m following this guide. In root’s .htaccess I added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^images/(.*)$ /web/images/$1 [L]
RewriteRule (.*) /web/$1 [L]
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<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
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation. It’s the first block of rules capturing all the pages, but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs. I checked mod_rewrite docs but couldn’t understand what’s wrong. Any help is appreciated. Thanks
RewriteBase /subfolder
You cannot set multiple RewriteBase directives in the same .htaccess file. The last instance "wins" and controls the entire file. So, in the .htaccess file you posted, RewriteBase / set in the WordPress code block, is what is actually set for the file.
However, none of the directives actually make use of the RewriteBase directive anyway - so none of the RewriteBase directives are actually doing anything. The RewriteBase directive only applies where you have set a relative path (not starting with a slash) in the RewriteRule substitution string.
but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs.
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
Presumably it's these two RewriteCond directives you are referring to... in which case these two conditions aren't really doing anything. RewriteCond directives only apply to the first RewriteRule directive that follows, so it only applies to the directive that rewrites your assets.
However, this RewriteRule is matching /assets in the document root, not /subfolder/assets, which is presumably the requirement - so these rules will fail to match.
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation.
The rules will certainly "break the blog installation", however, they don't appear to get as far as handling the request "through Yii". There's nothing that actually rewrites the request to /subfolder. However, the following directive unconditionally rewrites everything to the /web directory in the document root (which presumably does not exist) - so this will certainly "break" all the WordPress URLs.
RewriteRule (.*) /web/$1 [L]
In fact, I would have expected this to have created a rewrite-loop (500 Internal Server Error response)?! Unless you have a subdirectory /web off the document root which also contains an .htaccess file containing mod_rewrite directives? But that seems unlikely, since the /web directory should be inside the /subfolder directory?
Try the following instead:
RewriteEngine On
RewriteRule ^(subfolder)/(assets|css|js|images)/(.*) $1/web/$2/$3 [L]
RewriteRule ^(subfolder)/((?!web).*) $1/web/$2 [L]
RewriteRule ^subfolder/web/index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subfolder)/. $1/web/index.php [L]
# BEGIN WordPress
:
No need for the <IfModule> wrapper. Or the RewriteBase directive.
Alternatively
However, it would be preferable to move these directives into their own .htaccess file in the root of the project, ie. /subfolder/.htaccess - which I believe is what the linked "guide" is suggesting. This keeps the two projects entirely separate. And avoids having to explicitly include the /subfolder in the directives.
In addition, creating a another .htaccess file in the web subdirectory, ie. /subfolder/web/.htaccess. This is again, suggested in the linked "guide". However, this also negates the need for the additional directives to route the request in the parent .htaccess file.
For example, putting these changes together, the /.htaccess file in the document root should only have the WordPress directives. And then...
/subfolder/.htaccess
RewriteEngine On
RewriteRule ^((?:assets|css|js|images)/.*) web/$1 [L]
RewriteRule ^((?!web).*) web/$1 [L]
/subfolder/web/.htaccess
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Again, no need for the RewriteBase directive here - in fact, using RewriteBase here arguably complicates things. When in the /subfolder/web/.htaccess file, all relative URL-paths are relative to that directory.
So, requesting /subfolder/foo gets internally rewritten by the /subfolder/.htaccess file to /subfolder/web/foo. Which is then caught by the /subfolder/web/.htaccess file (preventing a rewrite loop) and internally rewritten to /subfolder/web/index.php (providing foo does not exist as a physical file).

How to point my domain directly to a codeignter folder?

Let's say I have a domain www.example.com
Now I want that, at this domain, my index page or main controller of the CodeIgniter folder should open without the folder name and the rest controller pages also without the folder name.
From: www.example.com/folder
To: www.example.com
I am hosting my files on hostinger. I don't want to remove my folder and export all the structure to base public_html.
I want my public_html to contain my CodeIgniter folder.
Any help would be appreciated!
To setup codeigniter on a subdirectory, you could try the following htaccess rule :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /YOUR_SUBFOLDER
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
Change the YOUR_SUBFOLDER above to whatever subdirectory name you have.

How do i use htaccess rules to rewrite to a certain file?

I have to create a page handler, which should read the URL, and do specific operations based on querystring.
I'd need to use .htaccess to do some URL rewriting thing to point everything at a certain file which does the processing, in such a fashion:
https://example.com/folder/page1/
https://example.com/folder/page2/
And the processing file is https://example.com/folder/index.php
Is there any way to do that (possibly by removing the index.php part)?
try this out and see if this helps you achieve what you are looking for.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /folder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
The above code is taken for reference from WordPress and it should work when the "RewriteBase /folder/" folder name is updated with your folder and .htaccess file has to be placed in the root/folder directory where your index.php file is located.

.htaccess - point primary to subfolder and remove it + rewrite for "pretty urls"

I am having trouble getting this to work without seeing my sub-directory in the URL...
# This is in root directory
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
Rewriterule ^(?!abc/)(.*)$ /abc/$1 [L,NC]
and
# This is in sub-directory
RewriteBase /pages/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} url=pages/(.+?)\.php
RewriteRule ^index\.php$ /%1? [R,L]
RewriteBase /pages/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /index.php?url=pages/$0.php [L]
The problem is, to make this work I have to have my base url set to "mysite.com/abc/content" instead of just "mysite.com/content". I'm pretty sure the answer is that I can't have my cake and eat it too (due to the nature of the redirects), but wanted to check before I move this site back to the server root.
It looks so nice and organized having it in one folder like the other sites on my server :(
Example
-public_html
--site1
--site2
--site3
--abc
My host is set up to look in public_html for the primary domain. There is no way to change this except for redirect in the public_html via .htaccess.
If I redirect to the sub-folder (/abc/), then I have to make my base href mysite.com/abc/. This is undesirable. I want my <base> to work with mysite.com/, and the sub-folder to never be visible in the URLs.
It seems this is impossible with the way it's set up. I just took the primary site's contents and dropped them into public_html for now - but it's ugly.

Htaccess redirect with cakePHP

I have a cakePHP app working at http://domain1.com/domain2, and want to point http://domain2.com/ to this application.
I have done this change the document root of domain2 to public_html/domain2, but, when I go to: domain2.com all css, javascript and images are not loaded, with a message saying controller not found.
What I can do?
All domains have a folder with their domains without the TLD at domain1.com, I think if I create a htaccess and get the domain without TLD, and finally change rewriteBase, will work as expected, but don't know how to do this.
Current htaccess of domain2:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
You can try adding an htaccess file in domain2's document root with the following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(css|js|png|jpe?g|gif|bmp|ico)$ [NC]
RewriteRule ^/?domain2/(.*) /$1 [L,PT]
This should remove the domain2 part of the links.

Resources