Prevent .htaccess rules to the child folder - php

Here is my folder structure
www.mywebsite.com
www.mywebsite.com/app
www.mywebsite.com/app/firstproject
www.mywebsite.com/app/secondtproject
In the root directory i installed wordpress. It allows me to access the www.mywebsite.com/app but when i try to access the www.mywebsite.com/app it is always showing page not found i can guess that it is because of the .htaccess at the root folder.
Here is the .htaccess root folder
# 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
I tried to override the .htaccess to the sub folders by having the below code in the .htaccess of the app folder but it shows the same page not found error.
RewriteRule ^app/ - [L]
How can i access the www.mywebsite.com/app/firstproject without that error ?

Remove app/.htaccess
Have this code in root .htaccess:
root .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php$|app/) - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress

Write all rules in the root htaccess file.
add
RewriteRule ^app/firstproject$ - [L]
to prevent rewrite in this path.
For further rewrite within /firstproject, you need conditions and rules similar to web root
RewriteEngine On
RewriteBase /
RewriteRule ^app/firstproject$ - [L] #add longer path first
RewriteRule ^index.php$ - [L] #no need to escape \.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Related

Laravel .htaccess subfolder

I now have a Laravel installation with the default folder structure.
In my root folder I have a .htaccess file like this one.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^ index.php [L]
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I have to install in a subfolder WordPress.
So I would like to access to my WP installation using the URL http://www.example.com/ and to Laravel with http://www.example.com/admin.
The thing I know it's that is an .htaccess trick, but, even if I tried many kind of rewrites, I'm not still able to achieve the desired result.
So, in your root .htaccess you should use the following:
RewriteEngine On
# Route any request begining with 'admin/' to laravel
RewriteRule ^admin/(.*)$ public/$1 [L]
# Anything else goes to Wordpress
RewriteRule ^(.*)$ wp/$1
And in your Laravel .htaccess add:
RewriteBase /admin
If you can't live with the trailing slash being mandatory you can try:
RewriteRule ^admin(/(.*))?$ public/$2 [L]
Try this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /your-subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /your-subdirectory/index.php [L]
</IfModule>
# END WordPress

Redirection of website-want to use /en folder being redirected to post

I encountered a strange behaviour with WordPress website.
I have created /en folder in the root and want to have another WordPress website there.
The problems appears when I try to open mainwebsite.com/en I'm being redirected to mainwebsite.com/english-post
So, WP gives the priority to the post that is found in the main domain and it's slug begins with "en".
I tried changing the folders name to "ren" and the website in the folder than shows up nicely.
I've checked .htaccess file for some redirects and none is set.
I noticed that this is a typical WP behaviour, as I tested on some "bigger" brands that use WP and same thing happens,ie:
https://www.smashingmagazine.com/en redirects to --> https://www.smashingmagazine.com/2014/11/enabling-multiscreen-tracking-with-google-analytics/
http://vanheusen.com/en redirects to --> http://vanheusen.com/products/english_shaded_box_silk_tie/
Have any idea how to solve this?
This is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(en/)
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try using this rule in your main .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And then you need this rule below in the .htaccess inside en folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /en/
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /en/index.php [L]
</IfModule>
# END WordPress
Let me know how this works

Make HTACCESS to not be applied for some subfolders

I am using this htaccess for my application.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any URL I will type in my browser, if the respective file doesn't exist, it will open the index.php.
The problem is I have many applications like this installed on the same domain.
For example, the root contains an index.php file and a htaccess like above.
I have also, /store, which contains a index.php file and a htaccess like above, too, but when I access /store/something it opens the index.php of root (not the correct /store/index.php
How can I solve this problem (how to make the htaccess of root to not override the htaccess of /store)? Thanks!
You can have a skip directory rule to skip all the directories:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# skip index.php and some directories
RewriteRule ^(store/|site1/|site2/|index\.php$) - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess -> index.php in web folder (url has to be web/)

I'm having problems setting up my .htaccess file. In my web folder (in the root of my project) I have index.php . Now I have to go to mydomain.com/web/ to see my index.php .
I've added a .htacces file in my /web folder but it isn't working. This is the content of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
What am I doing wrong?
You can have 2 .htaccess for this:
root .htaccess (a level above web):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!web/).*)$ web/$1 [NC,L]
/web/.htaccess:
RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
Have you tried RewriteBase ?
RewriteBase /web/
This post can be also helpful
How does RewriteBase work in .htaccess

Remove index.php from Codeigniter URL in subfolder with Wordpress in root

I'm having some issues with a Codeigniter app in a subfolder on my server along with a Wordpress install in the document root. If I hide the index.php of the Codeigniter URL with .htaccess
/codeigniter/.htaccess
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
And get Wordpress to ignore /codeigniter with RewriteCond %{REQUEST_URI} !^/(codeigniter).*$
/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(codeigniter).*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Then navigate to www.mysite.com/codeigniter/my_controller/my_function, I get a 404 error No input file specified. I found here, that adding a ? after RewriteRule ^(.*)$ index.php solves the Codeiginiter 404 error.
/codeigniter/.htaccess
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
However I'm now getting a Wordpress 404 error, so it seems if I replace index.php with index.php? in /codeigniter/.htaccess, the Wordpress rewrite rule in /.htaccess, RewriteCond %{REQUEST_URI} !^/(codeigniter).*$ gets ignored. Is there a better way to handle this?
Thanks in advance!
The RewriteBase in the codeigniter directory should say /codeigniter/
RewriteBase /codeigniter
Otherwise the rewrite to index.php ends up going to wordpress' router.

Categories