Exclude a folder/directory from RewriteRule - wordpress

I have a pretty standard WordPress .htaccess with the following URL rewrites
# 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
WordPress is installed in my domain root. I have some other scripts in subfolders, e.g. /opencart These subfolders contain their own .htaccess files.
Unfortunately, it seems that WordPress hijacks the rewrites for some of these scripts sometimes.
How can I ask mod_rewrite to ignore WordPress rules for rewrites when encountered with specific subfolders e.g. opencart and to use the rules defined in the .htaccess within these subfolders instead?

You may try replacing the complete WP rule-set with this one:
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI} !(folder1|folder2|folder3) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

In the .htaccess file in your site root, add the following ABOVE the WordPress .htaccess directives:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/subdirectoryname1/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/subdirectoryname2/(.*)$ [OR]
RewriteRule ^.*$ - [L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^/(folder1)/(.*) /folder1/$2 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,R]
</IfModule>
# END WordPress

Related

subdomains for different wordpress directory

I have staging.namexx.my and staging2.namexx.my now.
for staging.made4u.my, it's using directoryA
And now, i want staging2.namexx.my to use directoryB to open wordpress
*staging.namexx.my is currently working, just need to let staging to use directoryA, and staging2 to use directoryB in same server.
I'm playing with the htaccess and google for some time but still cannot get it.
I need some help.
my htaccess in root
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(staging.)?namexx.my$
RewriteCond %{REQUEST_URI} !^/dicrectoryA/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dicrectoryA/$1
RewriteCond %{HTTP_HOST} ^(staging.)?namexx.my$
RewriteRule ^(/)?$ dicrectoryA/index.php [L]
</IfModule>
# END WordPress
htaccess in directoryA
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dicrectoryA/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /dicrectoryA/index.php [L]
</IfModule>
# END WordPress
htacesss in directoryB
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directoryB/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /directoryB/index.php [L]
</IfModule>
# END WordPress
Thank you!
To point staging2.namexx.my to directoryB via rewrite , you could use the following rule in your root/. htaccess .
RewriteEngine on
RewriteCond %{HTTP_HOST} ^staging2\.namexx\.my$ [NC]
RewriteRule ^ /directoryB%{REQUEST_URI} [L]
Just replace staging2.namexx.my with your real domain name and keep the rule block at the top of your htaccess.

Wordpress permalinks problem installed in laravel with apache

I have a laravel application in: domain.com/co/app that controls it routes/web.php with code Route::post('/co/app'... I have installed wordpress in the public folder of laravel (laravel/public/co) resulting in the blog: domain.com/co.
But when I activate the permalinks in wordpress the lavarel application (domain.com/co/app) I get a wordpress 404 error.
I have ignored the path of the application from the wordpress .htaccess RewriteCond %{REQUEST_URI} !(app) [NC] but now I get an apache 404 error (The requested URL was not found on this server) and it does not show the laravel application.
This is the code of the .htaccess wordpress (laravel/public/co):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /co/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /co/index.php [L]
</IfModule>
This is the code of the .htaccesss de public (laravel/public)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
This is the Route code of the Laravel application (laravel/routes/web.php)
Route::post('/co/app', [
'uses' => 'App#aaa'
])->name('app');
Thanks for the help
I found the answer, in this special case, first you do not have to edit anything in the .htaccesss of laravel/public you only have to edit the wordpress (laravel/public/co). Ignoring the laravel application path with RewriteCond %{REQUEST_URI} !^/co/app and adding a new module that points to the index.php of laravel/public
The problem is that WordPress can intentionally edit your .htaccess and delete our modification, for that we use a hack that is explained here.
My final wordpress .htaccess code is:
<IfModule mod_ignore_wordpress.c>
# 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
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /co/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /co/index.php [L]
</IfModule>
# END WordPress
</IfModule>
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /co/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/co/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /co/index.php [L]
</IfModule>
#new module
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /co/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Redirect directory (but not subdirectories) to another directory

How do I redirect a directory to the root of a site? Provided that:
the root of the site is actually not the root of the domain. ex: it should redirect to 'www.example.com/website', not to 'www.example.com'
don't redirect subfolders. ex: it should redirect 'www.example.com/website/portfolio' to 'www.example.com/website', but it shouldn't redirect 'www.example.com/website/portfolio/project-name'.
It's a WordPress site and it's already working with the following rules:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /website/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /website/index.php [L]
</IfModule>
# END WordPress
Thanks in advance!
You can do:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /website/
RewriteRule ^portfolio/?$ /website/ [L,R]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /website/index.php [L]
</IfModule>

Want to run wordpress in a subdirectory and keep my main site separate in the root

I have an eCommerce website in the root directory and want to move my blog to a subdirectory. I successfully made the move from another domain to this one but when I attempt to go to the blog home page it re-directs to my eCommerce site's home page.
The eCommerce site - http://www.heavytshirt.com
The blog location - http://www.heavytshirt.com/blog
The .htaccess file in the blog directory looks like this:
RewriteEngine on
RewriteBase /
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
And the .htaccess file in the root directory looks like this:
DirectoryIndex /mm5/merchant.mvc?Screen=SFNT
RewriteEngine On
RewriteRule ^mm5/admin.mvc? - [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^product/([^/.]+).html$ /mm5/merchant.mvc?Screen=PROD&Product_code=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^category/([^/.]+).html$ /mm5/merchant.mvc?Screen=CTGY&Category_code=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^product/([^/]+)/([^/.]+).html$ /mm5/merchant.mvc?Screen=PROD&Category_code=$1&Product_code=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/.]+).html$ /mm5/merchant.mvc?Screen=$1 [L]
### End - Inserted by Miva Merchant
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^heavytshirt.com [nc]
RewriteRule ^(.*)$ http://www.heavytshirt.com/$1 [r=301,nc]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
Currently I am able to type in a subsequent sub directory and get to a post - Like:
http://www.heavytshirt.com/blog/category/summershirts/
What can I do to make the blog home page come up when someone types in http://www.heavytshirt.com/blog
And not affect the main site at www.heavytshirt.com
I'm not looking into your apache configuration, since I believe it can easily be configured within WordPress.
Go into your WordPress administration panel, and move to Settings / General Settings.
Now change the values of WordPress Address (URL) and Site Address (URL) accordingly. Read this WordPress Codex article for more details.
Your issue is because you have set
DirectoryIndex /mm5/merchant.mvc?Screen=SFNT
Because /blog/ is a directory and the rules you have set are matching that as the directory index for /blog/ (because the directory actually exists).
You will have to combine the two Rewrite Directives to get the desired result (you don't need RewriteEngine On in there twice).
Probably better that you start from scratch testing line by line to make sure you get the desired result.
I can't test this, but you get the idea:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^mm5/admin.mvc? - [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^product/([^/.]+).html$ /mm5/merchant.mvc?Screen=PROD&Product_code=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^category/([^/.]+).html$ /mm5/merchant.mvc?Screen=CTGY&Category_code=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^product/([^/]+)/([^/.]+).html$ /mm5/merchant.mvc?Screen=PROD&Category_code=$1&Product_code=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([^/.]+).html$ /mm5/merchant.mvc?Screen=$1 [L]
RewriteCond %{http_host} ^heavytshirt.com [nc]
RewriteRule ^(.*)$ http://www.heavytshirt.com/$1 [r=301,nc]
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

Override Wordpress Url Rewriting

I'm trying to override wordpress url rewriting for 1 specific file. My current wordpress install rewrites .html files to the according .php extension. So, for a permalink in MY wordpress installation might look like www.mysite.com/mypage.html.
I have an ACTUAL HTML file i want to show, however the htaccess keeps auto rewriting it by default, here is the code i'm trying to use to IGNORE this specific file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/forex-system-demo\.html$ [NC] #<-- This is the file i want to ignore and run as a NORMAL .HTML file
RewriteRule . /index.php [L]
</IfModule>
I may have had some wierd browser caching going on because what is below now works after clearing cache.
Here is the SOLUTION:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^forex-system-demo.html$ - [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Here is the SOLUTION:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^forex-system-demo.html$ - [L] # <- this is where I tell wordpress to ignore this url
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Resources