How to remove directory path form image url using htaccess? - image

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.

Related

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

Peroblem with laarvel project deployment to cpanel

Hope you guys are well, I have faced a problem deploying my laravel project on Cpanel. When I am hit my route URL or a root URL then it show me "404 not found"
This is the root url : https://laravel.tamimikbal.com/project1/
I have written this code on my root .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
And this the public/.htaccess file
<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>
The .htaccess is not redirecting properly to the public directory. I can access and navigate on your site using "https://laravel.tamimikbal.com/project1/public/".
Try this as the contents of the .htaccess file in your root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I can better understand if you put how is the routing url should be looks like
anyway you can try this code:
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
while you don't have www. and https is activated on your domain you can use this code to make redirect from www. to none www and from http to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
the rout request and file directory you can use this code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css|webp|JP2)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
if you want to redirect you system files to 404 page edit this code and replace your system folder and your 404 routing page
RewriteRule ^system/(.*) index.php?route=your 404 page [L]

How to redirect to public in laravel

I upload my laravel project from localhost to server but to access the app my url is http://myhostexample.com/public/ I want it to be http://myhostexample.com I have modified the public/.htaccess and here it is :
<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} !^public
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
In .htaccess you shouldnot change anything;
you need to follow following steps to deploy the laravel project:
copy the files and folder of public folder in your laravel project and paste it on public_html
now open public_html and open to edit index.php file
now to change the path of autoload and bootstrap in index.php to where those file present
now check your domain
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

How does laravel redirect first to public folder?

I am looking at a stock laravel 5.8 project and I am trying to understand how does Laravel redirect all the requests from the / to the /public folder.
There is no index.php file in the root and no .htaccess to redirect traffic to public?
laravel 5.8 i use the following
root .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
public/.htaccess
<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]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Editing .htaccess file, multiple domains

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

Resources