I'm struggling with .htaccess a little.
I have routes in routes.php like for:
/category/{categorySlug}
/product/{productSlug}
/tag/{tagSlug}
I need to use even nicer urls that looks like:
some-category.c.html --> /category/{categorySlug}
iphone-7s.p.html --> /product/{productSlug}
samsung-galaxy.t.html --> /tag/{tagSlug}
It should not use redirect, only identify url pattern ant continue further to routing with remade url. It should stay as it is on browser.
I have tried something like this with various variations with RewriteRule ^(.*).c.html$ /category/$1 [NC] but no success (all other config is default that comes with fresh Laravel 5.5 install):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Tried various flags...
RewriteRule ^(.*).c.html$ /category/$1 [NC]
# 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>
Thanks! :)
I'm not sure that trying to customize .htaccess is the best way.
If you want to have a route like this localhost/some-category.c.html instead of localhost/category/some-category.
You should try something like that :
Route::get('{categorySlug}.c.html','Controller#method');
Route::get('{productSlug}.p.html','Controller#method');
Route::get('{tagSlug}.t.html','Controller#method');
If you still want to edit the .htaccess, you can give a look to those links :
- How to change url? htaccess
- url rewriting using htaccess for category & product in php
Related
I'm writing a web application in laravel and have it almost working.
The only thing that is stopping me right now is the Rewrite Engine from apache ruining one particular route.
Route it should be: http://example.org/publicaties
Route it's
rewritten to: http://example.org/public
publicaties == publication but in dutch.
I'm using the following rewrite code as this is mostly the default for laravel if not completely:
DirectoryIndex index.php
<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>
Any help would be awesome!
Thanks
Solved
Turns out laravel doesn't like similar names like 'publicaties' and 'publications' because after changing the url, the issue was gone. Thanks all!
I have added a few routes to the api.php file. These routes are working perfectly in the localhost environment.
http://localhost/api/v1/events
gives desired output but when uploaded to the production/remote server I cannot access it using the domain name.
http://domainname/api/v1/events
throws an 404 error. I am using the same .htaccess files in the localhost as well as in the prod/remote server. I am not sure why is it not working correctly in the prod/remote server.
By the way, I have web routes which works fine in both the environments. My .htaccess file is copied below.
<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>
Modified .htaccess as per the answer but it is not working for me :(
<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}]
# API
RewriteRule ^api/(.*) api.php/$1 [L]
# 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>
Tried this as well.
RewriteRule ^api/(.*) routes/api.php/$1 [L]
Can anyone help by advising what is wrong?
My root folder
the folder in which api.php is available
The confusing part about this is that you say it works "perfectly in the localhost", yet you are using the same .htaccess file in both environments. This cannot work on localhost unless you have some other directives somewhere since there is nothing that would rewrite such a request to /api.php.
To enable this, and pass the additional path information as path-info to api.php, then you would need to add something like the following after the # Handle Authorization Header rule:
# API
RewriteRule ^api/(.*) api.php/$1 [L]
This would internally rewrite a request of the form /api/v1/events to /api.php/v1/events, allowing api.php to read /v1/events from the $_SERVER['PATH_INFO'] superglobal.
Without this directive (and with MultiViews being disabled) a request of the form /api/v1/events would be routed through index.php instead where I assume the "route" is not defined, hence the 404.
UPDATE#1: No my /api.php is under the same directory which has web.php, that is routes
In that case, you need to modify the above rule to read:
RewriteRule ^api/(.*) routes/api.php/$1 [L]
UPDATE#2: from your updated URL structure, it seems that /routes is actually under /myprojectfolder in the document root (where the .htaccess and index.php files are located), so you would need to modify the above to account for this. For example:
RewriteRule ^api/(.*) myprojectfolder/routes/api.php/$1 [L]
However, it seems that a direct request for /myprojectfolder/routes/api.php/v1/events does not work anyway, so passing this as path-info would not seem to be the correct course of action to begin with.
I have two project folder, e.g. blog1 and blog2.
/var/www/html/blog1
/var/www/html/blog2/laravel
I am doing it as sub folder instead of sub domain.
blog1 folder consists of laravel project. Inside blog2 folder, there's is a folder name laravel which consist of laravel project.
this is my htaccess for blog1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^(.*)$ blog1/public/ [L]
</IfModule>
Every time access too blog1 is simple, just https://website.com/login
Every time i want to access blog2 project, this is the link looks like https://website.com/blog2/login
htaccess inside blog2
<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 ^(.*)/$ / [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>
How can i make the link looks like https://website.com/blog2/ or https://website.com/blog2-sg/ or https://website.com/blog2-uk/?
My expected result
https://website.com/blog2/ will go to laravel login page
https://website.com/blog2-uk/ will also go to laravel login page
They will route to /var/www/html/blog2/laravel
Can i know is this doable and how to achieve it? Thanks.
I suggest looking in to Laravel Routes Subdomain Routing example:
https://laravel.com/docs/6.x/routing#route-group-sub-domain-routing.
This demonstrates how to use variable/dynamic parts in the URI.
Point all /blog2-*/ URIs to the Laravel application and let Laravel do further routing. I don't know your exact situation, but maybe the following redirect rule could help you out:
RewriteRule ^blog2-(.*)/ http://www.website.com/blog2 [R=301,L]
so i am using cloudways with digitalocean and now trying to add letsencrypt into my laravel 5.4 web app...
and according to tech support in cloudways they say i need to modify my .htaccess to be like this
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
<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>
but after that i got
ERR_TOO_MANY_REDIRECTS
what's gone wrong? cloudways tech support can't give any solution either... so i kinda stuck in here
i also do some research before and read that i need to add
If(env(' APP_ENV') !== 'local') { $url->forceSchema('https'); }
into app/Providers/AppServiceProvider.php in boot function to make all request to be https....
but still no luck...
okay so i already got the solution, i forgot that my site also have cloudflare cdn and in crypto part it set SSL to Full mode, just change it to flexible one and everything works great....
I just found the problem on server. all is working fine in my localhost, but on live server, ONLY the home page route is working.
My directory is:
laravel-
css
js
local->
app
HTTP->
Controllers->
Homecontroller
admin->
Groupcontroller
config
...
Here is my htacess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php [L]
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
And my route file is:-
Route::get('/group/detail', 'Groupcontroller#index');
Route::get('/group/add', 'Groupcontroller#create');
Route::get('/group/edit/{id}', 'Groupcontroller#edit');
http://www.example.com/home
My home controller is working.I think issue is with admin folder???
http://www.example.com/admin/group/detail
This is not working
Error is encountered :-
Class App\Http\Controllers\Admin\Groupcontroller does not exist
Please help me,Working fine at localhost but not on live.
Thanks in advance
Try and change the content of your .htacess to this
<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>
Might be you forget to paste .htaccess file from /public folder to /public_html folder
so Just COPY And PASTE the .htaccess file from public folder to /public_html folder
then it will work as local
Path of Groupcontroller is Controllers/admin/Groupcontroller. So in your routes you need to access Groupcontroller with appropriate path.
Route::get('/group/detail', 'admin\Groupcontroller#index');
Route::get('/group/add', 'admin\Groupcontroller#create');
Route::get('/group/edit/{id}', 'admin\Groupcontroller#edit');
Also it is recommended to use CamelCase folder names. that is; change admin => Admin.
Check the namespaces compared to directory names. The cases should match.
You can try the bellow code :
After the domain name add "public" keyword between your API routes
for example:-
https://example.com/public/api/someroute
After this, you may have to update your PHP version .