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.
Related
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>
I'm in the process of trying to prettify my URLs
From: http://localhost/blog.php?id=1
To: http://localhost/blog/1
I've added the below to my .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^blog/([a-zA-Z0-9]+)$ blog.php?id=$1 [NC,L]
From my understanding that should now redirect when I call my "to" url in PHP header("location: /blog/".id); it takes me to the relevant page, and my $_GET['id'] should have a value. It's coming up empty
I've been messing around with a few different regular expressions, but I don't think that's the issue.
In the apache config I changed "AllowOverride" to 'All'
To check to see if the .htaccess was even being used I put in some invalid regex, and got an error in apache_error.log
I've been on a ton of different pages asking the same thing, and watched plenty of YouTube vids, but I can't see what I'm missing
Have your htaccess Rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
Options +FollowSymLinks -MultiViews
RewriteEngine On
##Rules for external redirect to blog/1 here.
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(blog)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rules for internal rewrite to blog.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([\w-]+)/?$ blog.php?id=$1 [QSA,NC,L]
##Rules for internal rewrite to index.php file here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ index.php?id=$1 [QSA,NC,L]
try this .
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule blog/id/(.*)/ blog.php?id=$1
RewriteRule blog/id/(.*) blog.php?id=$1
example url :- http://localhost/blog/id/1/
Directory Type URL
I am trying to get
/pages/Settings.html
to appear as
/Settings
by using the RewriteRule
RewriteRule ^([A-Za-z_]+)$ pages/$1.html [NC] # Handle pages
in the .htaccess file. This does not work. However,
/pages/ANYOTHERFILE.php
works by using
RewriteRule ^([A-Za-z_]+)$ pages/$1.php [NC] # Handle pages
to be rewritten into
/ANYOTHERPAGE
Why does the former not work and the latter work? How do I get .html extensions to be rewritten like this?
Add this rules to top of your .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pages/$1.html [L]
I'm trying to remove the ".php" extension from the URLs of a site using this code (which I admit I copy/pasted from other questions here) in the .htaccess file:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Other lines of the .htaccess file do work, for instance, I have an error redirect and:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
So, I know the .htaccess file is in service in general.
I don't know what could go wrong in this area, so I'm not sure where to begin troubleshooting. Does anyone have pointers?
Thanks in advance.
Given that your domain account is /home/youraccount/public_html, your .htaccess would be inside the public_html folder with the following content:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
# First we redirect the www to non-www
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^/?(.*)$ http://domain.com/$1 [R=301,L]
# now we redirect phpless URLs internally to the php
# if folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# but the file exists and ends with .php
RewriteCond %{REQUEST_FILENAME}\.php -f
# redirect to name.php
RewriteRule ^([^/]+)/?$ $1.php [L]
NOTE: If you have more rules this may conflict so I would have to look at the rest of your rule but basically the above should work as expected.
You will be able to access both:
domain.com/index
and
domain.com/index/
And it would redirect to your file index.php.
I'm trying to mod re-write to a specific sub folder called /checkout this has an SSL certificate attached for https
So I have this in the root folder of my site -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
# this kinda works below but is resolving to index2.php and is showing in the url so not really working yet
RewriteRule checkout/([^/]+)\.html$ https://mysite.com/checkout/index2.php [L]
Except that instead of showing something like
https://mysite.com/checkout/index.html
it shows
https://mysite.com/checkout/index2.php
(index2.php is a test url btw)
I've also tried amending the .htaccess in the sub folder to to something like
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
RewriteRule ^([^/\.]+)/?$ https://mysite.com/checkout/index2.php [L]
I just want to process
https://mysite.com/checkout/index.html or https://mysite.com/checkout/about-us.html as the same I would in the root like this
https://mysite.com/index.html => https://mysite.com/index.php (and then get the variables etc)
ie
RewriteRule ^/?(catalogue-page)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+) index2.php?friendly_url=$1&catalogue_id=$2&view=catalogue&categoryid=$3&subcategoryid=$4&subjectid=$5&item=$6
I think that https is more complex.
Any pointers/help etc much appreciated
Your RewriteRule seems to indicates that you are trying to take a non-secure url and display a different page via https, this in itself is not possible. Having said that your description doesn't sound like that is the overall aim.
If you wish to force non-secure URLs to be secure, this is fine and it's also fine to want requests for index.html to server index.php. It's not however possible to do this all in one fell swoop.
# first force urls to be secure.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^checkout/([^/\.]+)/?$ https://mysite.com/%{REQUEST_URI} [R=301,L]
# now rewrite them, so that the requests all get served through the front controller
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^checkout/([^/\.]+)/?$ /checkout/index2.php [L]