Symfony2 - redirect /web directory to root - .htaccess

I've got a duplicate content problem with my Symfony2 projet. The following urls gives the same content :
www.mywebsite.com/web/page and www.mywebsite.com/page
Here is the content of my /.htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
And the content of my /web/.htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I would like to redirect any url starting with /web to / but I can't manage to do it. Do you have any suggestion ?

In the htaccess file in your web directory (the /web/.htaccess file), add these rules right beneath the RewriteEngine On:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /web/
RewriteRule ^(.*)$ /$1 [L,R=301]
This redirects all direct access to the web directory to the root.

Related

How to redirect to HTTPS in htaccess file in my framework setup?

I've built a php website working all fine with all the redirects except for http:// versions.
So I'm trying to edit my main .htaccess file to prevent http:// showing the error "NOT SECURE", and forward the user to https://
I've used a framework called TraversyMVC while building my website and it has the setup below in the public_html directory .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And this is my .htaccess file in the /public directory
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
This is what I've tried in my public_html directory .htaccess file but it didn't work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
</IfModule>
Looking for the correct solution.
Thanks
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} public/ [L]
RewriteRule ^(.*)$ https://www.mywebsite.com/public/$1 [L]
You are redirecting the request before the internal rewrite to the /public subdirectory (so the second condition that checks for public/ will never match) and /public should not be in the visible URL you are redirecting to. You should also be explicit that this is an external redirect, not an internal rewrite. (As written this rule would result in an implicit 302 - temporary - redirect.)
This needs to go before the existing rules in the root .htaccess file. Try the following instead:
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Test first with a 302 (temporary) redirect to avoid potential caching issues.
You do not need the <IfModule> wrapper.

CSS & JS file not loading in my cakphp appliaction

I have a cakephp website hosted on goDaddy, the css and js file are showing 404 error and the page appears blank.
i have done changes to .htaccess file in both webroot and main directory
.htaccess file in www directory
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(\.well-known/.*)$ $1 [L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
and .htaccess file in webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
i'm not sure what is the cause.I checked the error log and its shows this
2020-09-02 08:06:25 Error: [Cake\Routing\Exception\MissingControllerException] Controller class Webroot could not be found.
Request URL: /webroot/
Try using this on the /webroot .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

.htaccess Redirect url to folder

I have a blog folder in my servers /home/app/sites directory, and I have my laravel app in /home/app/public_html/laravel and my domain www.laravelapp.com is pointing to the project.
I want to point the url www.laravelapp.com/blog to my blog folder in /home/app/sites/blog.
How can I do that?
Here is my .htaccess file:
<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}]
# Redirect blog folder (Not working)
RewriteCond %{HTTP_HOST} ^www.laravelapp.com/blog$ [NC]
RewriteRule ^(.*)$ /home/app/sites/blog [L,R=301]
</IfModule>
../ goes one folder down
So it should be ../../sites/blog
There may be restrictions with htaccess. If this not already work share your .htaccess file. You cant go behind your base url, the following question explains it very well.
Having links relative to root?

Cakephp CSS file not loading

I have a website working on one server. I am changing hosting, so I created a website on another server for testing.
Everything seems to be working, except CSS. It is not loading at all. I have 777 for tmp folder and mod re_write enabled.
I can see all cached files except for CSS.
Please advise and help
This is my .htacess file in www
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
This is my second htaccess inside app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
This is my third htaccess inside webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
and the url is 23.236.49.24
Add this line:
RewriteBase /
under
RewriteEngine On
in the 3 .htaccess files
Create .htaccess file in webroot folder.
And paste this code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

How do I get two htaccess rewrite rules to 'play nice' together?

In my website root I have the following to redirect to non www domain
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
in a subfolder named 'photography' I have this...
RewriteEngine on
RewriteRule ^show/([^/\.]+)/([^/]+)$ show.php?section=$1&photoid=$2 [L]
Anything inside the photography folder ignores the www removing rule. How do I get these two rules to both apply to folders/files within the photography folder?
Also... my root htaccess file has this...
# 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
Could it be interfering? I have a self-hosted wordpress blog but it's not in the root of the website, it's in a subfolder called 'blog' so I don't know why this rule is in my root's htaccess file. Can/should I move it?
Edit: Just to point out, in case it isn't obvious - I'm a complete noob when it comes to htaccess and mod_rewrite stuff. Does a htaccess file in a subfolder override any htaccess files nearer to the root than it? Or do the htaccess contents combine?
Edit 2: I have tried moving the second rule to the same htaccess file as the www removing rule as per the following code...
# 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
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,NC]
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ photography/show.php?section=$1&photoid=$2 [L]
If I then go to one of my photography pages it resolves to the intended url (http://notails.com/photography/show/pointofayre/260 for example) but the page is a 404.
If I manually add 'www' to that it undoes the other rule... (http://notails.com/show.php?section=pointofayre&photoid=260) and removes 'photography/' from it.
Add RewriteOptions inherit into .htaccess in your "photography" folder because right now all rewrite rules from parent folders are ignored (default behaviour).
Alternatively move rewrite rules from that subfolder .htaccess into root one (you will need to slightly modify your rule by fixing the path -- adding photography/ may be enough, depends on actual "photography" location)
UPDATE:
Your root .htaccess can be like this:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
# activate rewrite engine
RewriteEngine On
# we are in the root
RewriteBase /
# no www please
RewriteCond %{HTTP_HOST} ^www\.notails\.com [NC]
RewriteRule ^(.*)$ http://notails.com/$1 [R=301,L,QSA]
# photography
RewriteRule ^photography/show/([^/\.]+)/([^/]+)$ /photography/show.php?section=$1&photoid=$2 [L]
# WordPress rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Resources