Statically served NextJS website is adding a trailing slash on page refresh - .htaccess

I have a NextJS website statically served on an apache server and using an .htaccess file to setup the routing rules etc. This files looks like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [L,R=301]
</IfModule>
The problem I'm facing:
My internal links look like example.com/page-1, so when I redirect from example.com to example.com/page-1 everything is working well.
But when I target this page directly from it's URL or reload the page the server is adding a extra trailing slash to the URL. e.g. example.com/page-1/
This causes duplicate content within search engines /
What I'm looking for:
I'm looking for a solution in my .htaccess that prevents the server from adding a trailing slash on reloading the page. But URL's like example.com/about/page-1 should keep working.

I found a solution for my own problem. The problem was causes because I copied this .htaccess from an old React project where it was supporting the react-router.
Within my NextJS project I only had to remove the .html extension inside the .htaccess file. See example below:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>

Related

htaccess delete and ban all extensions and add / to the end of the URL

I am making a small cms in php. Through htaccess I would like to delete and block all extensions (.php .html. Htm etc.) and add a / at the end of the url.
ex. www.miosito/ article-name/
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ core.php
this is my current htaccess.
Sorry for my bad English and thank you very much in advance
With your shown samples/attempts, could you please try following htaccess Rules file. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
##Excluding Indexes and MultiViews options here.
Options -Indexes -MultiViews
##Rule for url http://localhost:80/test here.
RewriteRule ^(test)/?$ $1/category [R=301,L]
RewriteRule ^(test/category)/?$ $1.php [NC,L]
##Rule for forbidding html OR htm OR php extension files.
RewriteRule \.(html|php|Htm)/?$ - [F,NC,L]
##Rule for adding / at end of urls.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
##Rules for non-existing pages to be served here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [QSA,L]
Try out this code in site root .htaccess after completely clearing your browser cache:
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
# block direct access to .php, .html, .html extensions files
RewriteCond %{THE_REQUEST} \.(html?|php)[?\s/] [NC]
RewriteRule . - [F]
## Adding a trailing slash
RewriteCond %{ENV:REDIRECT_STATUS ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,NE,R=301]
# core handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . core.php [L]

Remove index.php and redirect to https sudomain

.htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Options -Indexes
I want to remove index.php from URL and also when someone enters test.xyz.com in the address bar it should redirect to https://test.xyz.com.
I tried some codes as below by commenting the above code and then the problem was that when I enter test.xyz.com it's automatically redirecting to https://test.xyz.com but when I go to some other link like https://test.xyz.com/user/list its showing error.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So I need a better .htaccess file which will remove index.php as well as redirect to https for all URLs in my subdomain.
NOTE: Need combined version of both the above .htaccess code.
You may use this code in your site root .htaccess:
DirectoryIndex index.php
Options -Indexes
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]
Test it after clearing your browser cache or test in a new browser.

.htaccess file hide GET request in URL

I am very new to .htaccess files and may have done mine wrong. I have been gathering snippets of code trying to get done what I want.
This is what I have in my file so far
I read this should be in your .htaccess file
Options +MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
This was added to force a redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This was added to remove .php at the end of all the files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This was added to remove the showing of /index when the home link was pressed
RewriteCond %{THE_REQUEST} ^.*\/index?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L]
I have tried a lot of ideas I found on both here and other sites from Google. I have had no luck hiding the GET request.
Currently, the URL looks like https://mysite.ca/event?id=123
What I would like it to look like is either https://mysite.ca/event/123
This is hosted on Godaddy and it is just a plain PHP site if that makes any difference.
Any ideas would be great!
First of all Options +MultiViews already removes .php extension so you don't need:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
About your problem try this Rule:
RewriteRule ^event/([0-9]+)?$ event?id=$1 [NC]
Which changes url from example.com/event/<number> to example.com/event?id=<number>
Although, if your site makes infinite redirects then you should change it to:
RewriteRule ^events/([0-9]+)?$ event?id=$1 [NC]
This happens if you have some includes/headers that redirect back to event and creates infinite loop.

Codeigniter remove index.php when redirected from non-www to www

I have added a force redirect from non-www to www on my codeigniter based web app.
After doing that it is working fine, but its showing an extra index.php in URL which I need to remove.
Here is my URL : http://www.testprofile.com (WORKING FINE)
Here is problem caused :
https://testprofile.com/result/184/1103511096450940jawadamin (non www site, it will automatically add www.)
opening the above URL redirects to www but also adds an additional index.php in URL which should be removed.
HERE IS THE CODE OF MY .HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^testprofile.com [NC]
RewriteRule ^(.*)$ https://www.testprofile.com/$1 [L,R=301]
</IfModule>
Please help me remove this annoying index.php
Thanks,
You missed to escape dot in right hand side of rewrite condition line. It should be like
RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]
or in your whole example
<IfModule mod_rewrite.c>
RewriteEngine On
#If in root of web server you can use next line or set it appropriate according to location on server
#RewriteBase /
RewriteCond %{HTTP_HOST} ^testprofile\.com [NC]
RewriteRule ^(.*)$ https://www.testprofile.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
If dot is not preserved by back slash character apache read it as wild card character. You can find some useful tips on this page.
Also, you can check useful .htaccess snippets here.

when using htaccess all the internal links are changed

i want to change my website urls exemple www.mysite.com/about.php to www.mysite.com/about/
i'm using this code in my htaccess file
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
i tried this one too
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
it works but all the page content becomes inside the /about/ its like i created a folder that does not exist so all the internal links are changed , if i have a link to post.php it goes to /about/post.php
all the included pages are not working too such as css and images
sorry my english isn't so good .
i finally found what i wanted ,i used this meta tag in the header section
`<base href="http://localhost/mysite/" />`
now everything works
ps: don't use localehost on webhost

Resources