Redirect From Pretty URL to .html URL - html

I have recently switched from using Wordpress to a static html only site. When I remade each page (there are only 6 pages total) I kept the same URL name.
Example: https://reactiongaming.us/alteriwnet/ is now https://reactiongaming.us/alteriwnet.html
My issues is that old links and Google search results contain the reactiongaming.us/alteriwnet/ version which results in a 404 error. I have tried various .htaccess methods for "pretty URLs but none worked. Usually, these questions were asked on how to redirect from a .html URL to a pretty URL, but I'm trying to do the opposite.
There are only 6 URLs I need to redirect in total from reactiongaming.us/example/ to reactiongaming.us/example.html. Is there an .htaccess or other method to achieve this?
I have tried:
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.html [NC,L]

You can use Redirect directive to redirect your old URLs to the new URL format
Redirect 301 /alteriwnet/ /alteriwnet.html
This will 301 redirect /alteriwnet to /alteriwnet.html .
To redirect multiple URLs with just a one liner code you can use RedirectMatch which uses a regex based pattern
RedirectMatch 301 ^/(alteriwnet|path2|path3)/?$ /$1.html
References :
Redirect directive
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
RedirectMatch directive
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirectMatch

Related

How do you use htaccess to redirect requests to a folder to a new subdomain?

I am moving an application from a folder off the root, to a subdomain. So I need requests to website.com/folder to redirect to folder.website.com. Below is what I tried, but it adds the original folder to the URL. For example, it does folder.website.com/folder/login, but what I need is folder.website.com/login.
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com
RewriteRule ^folder/(.*)$ https://folder.website.com/$1 [L,QSA,R=301]
If you need this rule to only be applied to the folder route, then give the following a try replacing the following rule:
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com
RewriteRule ^folder/(.*)$ https://folder.website.com/$1 [L,QSA,R=301]
WITH
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^website.com [NC]
RewriteRule ^(.*)$ http://www.website.com$1 [L,R=301]
RedirectMatch 301 ^/folder/(.*)$ https://folder.website.com/$1
You may already have the first 2 lines added.

htaccess why is site redirecting to //

In my htaccess I am redirecting all non https to https.
It works, but its also adding in an extra '/', so the url is
'https: //www.[MY SITE URL].co.uk//'
Why is this? To be honest, I don't really know what all this in my htaccess is doing, its copied from googling answers to 'redirect all requests to https'
My htaccess:
Options -MultiViews
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
#RewriteCond %{HTTP_HOST} ^(www\.)?jobooz\.com [NC]
#RewriteRule ^(.*)$ https://www.jobooz.co.uk/$1 [R=301,L,NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1
RewriteRule ^/?$ /php/index.php [NC,L] # Home page
I've also noticed any deeper urls like '/search-jobs/jobs-near-me' that I add to the url then get duplicated too, to '/search-jobs/jobs-near-me/search-jobs/jobs-near-me' when redirected to https.
All urls work fine if I go directly to the https version.
Any help appreciated, thanks.
The following rewrite rule has a problem:
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
The gist of why you are seeing repeated fragments in the rewritten URL is that (.*) matches everything, and that already includes the host and URI. Instead, you can try redirecting any incoming request on port 80 to HTTPS.
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

How can I Implement HTTPS for all pages Yii2

I want all pages of my website(website and restfull api) to be accessed via https. Following this post(Yii 1.1: URL management for Websites with secure and nonsecure pages), I understand that it is for a website that will have nonsecure and secure pages.
should I do it as explained the post or there is a specific way for website for which everything uses https?
You might need something like this in your .htaccess file
RewriteEngine on
RewriteBase /frontend/web/
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Redirect according to the URL

I have the following scenario:
I'd like to do a redirection system like this:
/redirect/place1 -> /page.php?place=place1
/redirect/testabc -> /page.php?place=testabc
/redirect/xyzw123 -> /page.php?place=xyzw123
The only way I can see now is to do subfolders in /redirect which have a index.php who redirects it. But I'd like that to be fully automatic.
Thanks for any answer!
If you want to redirect /redirect/(Anything) to /page.php?place=(Same as Anything)
You can use redirect command from mod_alias:
Redirect 301 /redirect/ /page.php?place=
or
you wan use the RewriteRule comand from mod_rewrite
RewriteRule /redirect/(.*) /page.php?place=$1 [L]
More informations:
[L] is the kind of redirect, you can add a status code if you want to do a redirect condition, like [L,R=301] will redirect sending 301 redirect permanent
here you can find all http code for the redirection
https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP
here you can find all the flags from the rewrite condition
https://httpd.apache.org/docs/2.4/rewrite/flags.html
If you want to redirect only if page or folder doesn't exist:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule /redirect/(.*) /page.php?place=$1 [L]
!-d mean not found as a folder
!-f mean not found as a file

modrewrite to remove the file extension but still load the page

I have a subdomain with .php pages in it. I just want to remove the .php I have written some code gathered from other posts on stack, so far I have this
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (\.php(.*)\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
this rewrites
subdomain.example.com/weddings.php
to
subdomain.example.com/weddings
However it also creates a 404 page not found error. Am I doing something wrong?
Solved it. anyone struggling with the same thing here is my solution
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
found it on
http://css-tricks.com/snippets/htaccess/remove-file-extention-from-urls/
a fantastic website.