Editing .htaccess file, multiple domains - mod-rewrite

Inside the public_html folder their are numerous folders of other domains and a .htaccess file. I need to edit the .htaccess file of a specific domain and not effect the other domains.
Here is how last few entries of the .htaccess file looks like
# 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
# 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
# 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 want to add this piece of code after RewriteBase but I don't want to screw up the other domains.
RewriteRule ^page/(.*) ads/page/$1 [R=301,L]
RewriteRule ^page/(.*)/ ads/page/$1/ [R=301,L]
How do I solve this?

You can specify different .htaccess files to use using AccessFileName in your VirtualHost directive:
<VirtualHost <ip address>:80 >
# ...
AccessFileName .htaccess.some_domain

Related

Laravel in primary, wordpress in subdomain, how to setup

I have the domain e.g:
maindomain.com
with an installation of laravel and .htaccess file as:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I also have an installation of wordpress 5.8.1 in the subdomain:
portal.maindomain.com
with currently "empty" .htaccess file as:
# BEGIN WordPress
# END WordPress
I have also tried in subdomain .htaccess as:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
and even :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /portal/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /portal/index.php [L]
</IfModule>
# END WordPress
with no luck.
Subdomain always produce:
Forbidden
You don't have permission to access this resource.Server unable to read htaccess file, denying access to be safe
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
the wordpress domain in wp_options table in database is defined as:
https://portal.maindomain.com
Any ideas on how to set .htaccess files for both installations to work?

How to remove 'public' from Laravel URL?

I have a default laravel 8 project. I can only access my files if i use '/public/' to acess the route,and i do NOT want that. i just want to go 'localhost/ProjectName/Route' and not 'localhost/ProjectName/public/Route'.
I have tried changing .htacess, sending it to root,renaming server.php to index.php and a million other solutions i have found on the web.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization MemberHeader
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Remove public URL from the path
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
this is my htacess
Make sure your webs server configuration has the public directory set as the root folder rather than the root folder of the Laravel project.
Replace your .htaccess file with following code. Please note, you don’t need to rename server.php to index.php or anything to remove /public name. Just using following code in your .htaccess file and you are good to go.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Result:
website.com/public/route to website.com/route
If you are trying to run laravel project in sub-folder within the root directory, above code won't work. Instead, use following code in your .htaccess file.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</IfModule>
Result: website.com/sub-project/public/route to website.com/sub-project/route

apache setup .htaccess rewriteRule with laravel

im setting up (laravelproj/public/.htaccess)
the example ones that I've worked on 2 different servers as follows:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://takaful.hsn93.com/$1 [R,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
now I'm trying it on cent OS apache (httpd) (if that matters)
and when i put in the link (http://example.com/) < apache returns the following:
i dont have any other problem except this link ^ with no parameters ^
Forbidden
You don't have permission to access / on this server.
so to make it work i'd to comment this line (condition where it checks if the requested isnt directory:
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
which make sense because / is of course a directory ..
but why did this .htaccess configuration worked on all other machines?
what is different on this machine that makes this condition returning a forbidden from apache
i found the problem is that httpd.conf
doesnt index (index.php) files just (index.html)
so i modified it
<IfModule dir_module>
DirectoryIndex index.html
DirectoryIndex index.php
</IfModule>

How to remove directory path form image url using htaccess?

http://example.com/abc/media
TO
http://example.com/media
How to set this URL with use of .htaccess?
Could you try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ abc/$1 [L]
</IfModule>
If u using only for image. You can try with:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^.*/abc/media/(.*)$ http://%{HTTP_HOST}/media/$1 [L,R=301]
</IfModule>
RewriteCond %{REQUEST_URI} ^/abc/$
RewriteRule .* - [E=MAGE_RUN_CODE:aaa]
RewriteCond %{ENV:REDIRECT_MAGE_RUN_CODE} (.+)
RewriteRule .* - [E=MAGE_RUN_CODE:%1]
RewriteBase /abc/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abc/index.php [L]/
This is my .htaccess file.
Now we need to remove /abc/ folder from image URL so we get all image URL from the Root directory.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^.*/abc/media/(.*)$ http://%{HTTP_HOST}/media/$1 [L,R=301]
</IfModule>
Already Try this but not working on my site.

Put angular and laravel under the same root folder (.htaccess)

I have this folder structure
--laravel
----public
------index.php
------uploads
--------image.jpg
--angular
----dist
------index.html
I want 3 things
1) http(s)://www.domain.com/public/uploads/image.jpg goes to laravel->public->uploads->image.jpg
2) http(s)://www.domain.com/api/test goes to laravel->public->index.php
3) http(s)://www.domain.com/every_other_url goes to angular->dist
I have create this .htacces file
DirectoryIndex index.html index.htm default.htm index.php index.php3 index.phtml index.php5 index.shtml mwindex.phtml
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^api/(.*)$ laravel/public/$1 [L]
RewriteRule ^public/(.*)$ laravel/public/$1 [L]
RewriteCond %{REQUEST_URI} !^/angular/dist/
RewriteRule ^(.*)$ /angular/dist/$1
</IfModule>
First and second requirements works properly. Now I need to fix the 3rd
(I'm not using the HashLocationStrategy)
DirectoryIndex /angular/dist/index.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^api/(.*)$ laravel/public/$1 [L]
RewriteRule ^public/(.*)$ laravel/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.(.*) /angular/dist/$1.$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /angular/dist/index.html [NC,L]
</IfModule>

Resources