htaccess - overwrite root htaccess with subdirectory htaccess - .htaccess

My php files are located at "htdocs /app/public/ " that I need to show on root of the website and wordpress is at "htdocs /wp/ "
Previously I used code below to "htdocs /app/public/ " requests as "htdocs/"
#laravel htaccess - location : /htdocs/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ app/public/$1 [L]
</IfModule>
I want to have access on wordpress at "htdocs /wp/ ", so I added code below.
# WordPress - htaccess below laravel htaccess code
<IfModule mod_rewrite.c>
RewriteBase /wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Is that possible to overwrite root htaccess on wp directory?

Related

How to remove part of a url using htaccess

I have looked at a good few similar questions on stack overflow but what I have tried doesn't seem to work.
'quotes' is a subfolder with a wordpress installation in it. So, the main root has a wordpress site in it and the 'quotes' subfolder also has a wordpress installation in it.
I have a path something like
https://example.com/quotes/uk/travel-packages
https://example.com/quotes/us/travel-packages
But I don't want 'quotes' to be in the url, it should just be
https://example.com/uk/travel-packages
I currently have this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /example/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /example/index.php [L]
RewriteRule ^quotes/(.*)$ /$1 [L,NC,R]
RewriteRule ^uk/(.*) /quotes/$1 [L]
RewriteRule ^us/(.*) /quotes/$1 [L]
</IfModule>
EDIT: I was trying this on my localhost hence the rewrite base being /example/ so I understand the confusion there now.
Here is the live server .htaccess file for both the root directory and the quotes subfolder in the root directory.
ROOT:
# 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
QUOTES SUBDIRECTORY
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quotes/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /quotes/index.php [L]
</IfModule>
# END WordPress
If /quotes is a subdirectory that contains the WordPress installation and you have already removed /quotes from the URL in WordPress itself then you need to configure two .htaccess files:
One in the document root that internally rewrites (unconditionally) all requests to the /quotes subdirectory.
And another .htaccess file in the /quotes subdirectory that routes the request to WordPress - containing the "standard" WordPress front-controller.
In the (root) /.htaccess file:
RewriteEngine On
# Internally rewrite all requests to WordPress subdirectory
RewriteRule ^ quotes%{REQUEST_URI} [L]
In the /quotes/.htaccess file:
RewriteEngine On
# OPTIONAL...
# Redirect any direct requests for "/quotes/<anything>" back to root
# NB: WP itself *must* already be correctly configured to omit "/quotes" from the URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# WordPress front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The check against the REDIRECT_STATUS environment variable is to ensure only direct requests are redirected and not rewritten requests by the WP front-controller - which would otherwise cause a redirect loop.
Alternatively, you have just a single .htaccess file in the document root (and remove the .htaccess file in the /quotes - WordPress - subdirectory). For example:
RewriteEngine On
RewriteBase /quotes
# OPTIONAL...
# Redirect any direct requests for "/quotes/<anything>" back to root
# NB: WP itself *must* already be correctly configured to omit "/quotes" from the URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^quotes/(.*) /$1 [R=301,L]
# WordPress front-controller
RewriteRule ^quotes/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
It seems from your question update that you also have a 2nd WordPress installation in the document root. It's not possible to have two separate WordPress installations and make them appear as a single WP site in the document root (ie. without specifying the subdirectory as part of the URL) unless there is some discernable difference in the URL structure between the two sites. Otherwise, you don't know which URLs should be routed to the WP installation in the subdirectory and which to the site in the document root.
Any with /us or /uk or any country extension in future should go to /quotes otherwise it should go to the route or whatever page is being called from the root site like for instance, https://example.com/contant-us should go to the root contact us page.
Yes, this is possible. For "any" country extension I assume a string of 2 lowercase letters a-z. Although this does mean that the WP site in the document root cannot have any URLs that start with a 2 letter path segment.
Ordinarily, you would modify the existing directives (as above). However, since this is WordPress, the directives in the # BEGIN WordPress code block should not be modified (unless you prevent WP from modifying this). Instead, we will just add some directives before the WP front-controller.
In the (root) /.htaccess file:
# Rewrite any URLs that contain a language code prefix to the subdirectory
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]
# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)
In the /quotes/.htaccess file:
# OPTIONAL...
# Redirect any direct requests for "/quotes/<anything>" back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)
This assumes you will be accessing your static resources directly. eg. Using the /quotes subdirectory for resources that are contained within this site, so the /quotes subdirectory is not entirely hidden.
See my answer to part two of this question with regards to missing static resources (images, CSS and JS) for the second WordPress installation in the subdirectory that could result from implementing the above rewrites/redirects.
wordpress site missing images after htaccess change

Configurating .htaccess file for my CakePHP 3 app located in a git repo subdirectory

Found a similar question
Cakephp 3 .htaccess File Configuration
but no answer there.
My GIT repo structure
ROOT
- .htaccess
- /htdocs (CakePHP 3 app)
- - .htaccess
- - /webroot
- - - .htaccess
ROOT .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ htdocs/ [L]
RewriteRule (.*) htdocs/$1 [L]
</IfModule>
htdocs .htaccess (default)
# Uncomment the following to prevent the httpoxy vulnerability
# See: https://httpoxy.org/
#<IfModule mod_headers.c>
# RequestHeader unset Proxy
#</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
htdocs/webroot .htaccess (default)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
https://***.com/bestprice.com/ (GIT repo location)
I am actually redirected to htdocs (htdocs not displayed in url, just like I need), but get a Missing Controller error.
Bestprice.comController could not be found.
https://***.com/bestprice.com/htdocs/ works fine
I have tried many different ways and this is the best result I could achieve.
Could you please tell me what I am doing wrong? Thanks a lot for your help

How do you redirect all request to public/ folder in laravel 5

I have a classic Larevel 5 project structure and I need to redirect all requests to public/.
I am on a classic hosting environment so public/ is a subfolder of my document root.
I shall imagine it can be done via .htaccess but I still need to figure out how. Anyone can help?
Thanks
There are two solutions:
1. Using .htaccess with mod_rewrite
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
2. You can add a index.php file containing the following code and put it under your root Laravel folder (public_html folder).
<?php
header('Location: public/');
You don't need to change anything in Laravel's default public/.htaccess file.
Just create a new .htaccess in the same level your public folder and add the following content to it:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
That simple!
This is an extract from another answer which may also help you.
--
Modify your public_html/.htaccess to redirect all requests to the public subfolder.
# public_html/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect All Requests To The Subfolder
RewriteRule ^ /public
</IfModule>
Make sure you have the proper public_html/public/.htaccess (GitHub).
# public_html/public/.htaccess
<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>
If you use cPanel, then:
1.Go to folder:
/var/cpanel/userdata/my_domain
2.Edit the both domains:
my.domain and my.domain_SSL
Add to the documentroot section /public:
documentroot: /home/user/public_html/public
3.Rebuild Apache config:
/scripts/rebuildhttpdconf && service httpd restart
if you using apache , add .htaccess file to your root directory with this lines :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
and make sure your apache redirect and rewrite modules is working fine .
you can use vhost to redirect the all request to your public directory with set public as root of your project .
The ideal scenario is to have /home/user/public as a symlink from /home/user/laravel/public.

.htaccess redirect & hide directory path in url

I have wordpress installed in root directory. I want to use some separate php files as a page on my domain, for which I have made a separate directory which holds all files for serving the php files, of which the directory structure is as:
the root folder has all the wordpress files
the directory which I'd like to serve as a page
/inc/css/
/inc/php/
/inc/img/
the CSS stylesheet files directory location in the PHP file is ../inc/css one step back & then css folder.
I want to hide folders from URL such as the files are served from the root (hide the /inc/php/, /inc/css/ & /inc/img/ folders from URL).
eg: www.domain.com/inc/php/about.php redirect & rewrite this URL to www.domain.com/about
the .htaccess in my root
RewriteEngine On
RewriteBase /
# disable directory browsing
Options -Indexes
# Prevent hotlinking of images htaccesstools.com/hotlink-protection
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [NC,F,L]
RewriteRule ^login$ http://domain.com/wp-login.php [NC,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
<files wp-config.php>
order allow,deny
deny from all
</files>
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
I have tried the simple redirect rule but the folders are exposed in the URL.
Redirect 301 /about.php /inc/php/about.php
also I have some more files in the PHP folder on which I'd like to apply the same rule of redirect & rewrite URL & hide folders from URL & remove the PHP extention.
www.domain.com/inc/php/about.php redirect & rewrite this URL to www.domain.com/about
This, of course, means that you can't have the same base filename that's a php file and, say, a css file. Since if the request is www.domain.com/about, is that supposed to map to /inc/php/about.php or /inc/css/about.css? Or is it an image? If you have both files, only one will get mapped to.
But if that's really what you want, try adding these rules right after the hotlinking rule that you have:
# Externally redirect requests for /inc/
RewriteCond %{THE_REQUEST} \ /inc/(php|img|css)/([^\?\ ]+)\.(php|css|jpe?g|png|gif) [NC]
RewriteRule ^ /%2 [L,R=301]
# Check if the request is a php file:
RewriteCond %{DOCUMENT_ROOT}/inc/php%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ /inc/php/$1.php [L]
# Check if the request is a css file:
RewriteCond %{DOCUMENT_ROOT}/inc/css%{REQUEST_URI}.css -f
RewriteRule ^(.*)$ /inc/css/$1.css [L]
# Check if the request is a jpeg file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.jpg -f
RewriteRule ^(.*)$ /inc/img/$1.jpg [L]
# Check if the request is a gif file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.gif -f
RewriteRule ^(.*)$ /inc/img/$1.gif [L]
# Check if the request is a png file:
RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.png -f
RewriteRule ^(.*)$ /inc/img/$1.png [L]

wordpress htaccess rewrite

I have a wordpress blog.
My question is. How to rewrite this url
www.1234.com/?s=&cp_state=city
On this
www.1234.com/city
Try adding these lines to .htaccess file in root directory before WP directives, if any, or any other directive. Example:
#These are the 2 lines to add
RewriteEngine On
RewriteRule ^(city)/?$ ?s=&cp_state=$1 [L]
#End of added lines
# BEGIN WordPress - This is just an example, there might be more or different.
<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 - WP directives end here
The requested URL can be www.1234.com/city or www.1234.com/city/ with or without www.
If necessary, in order to test redirection is working add something like this at the start of index.php:
<?php
if ($_GET['cp_state'] == 'city') {
echo "cp_state = city<br /><br />";
}
?>
The string "cp_state = city" should be displayed.

Resources