How to htaccess redirect urls to .html - .htaccess

I have been trying to find a code to 301 redirect pages like this in htaccess file:
www.example.com/view-profiles
To
www.example.com/view-profiles.html
But I need to make sure it does not conflict with the /index.html redirect rule I have there and also could be applied as a rule to many urls at once.
Thank you in advance for your help!!

Have you tried this?
redirect 301 /view-profiles http://www.example.com/view-profiles.html

If you want to have all the files that have a .html extension to be the target of such a redirect, you have to use mod_rewrite
This is untested!
For testing I recommend to first replace the 301 with a 302, because your browser might remember the 301 setting and won't access the server to be 'rewritten'.
In your .htaccess you need something like this
<IfModule mod_rewrite.c>
RewriteEngine on
# If in subdirectory uncomment the following and
# replace subdir with your subdirectory
#Rewritebase /subdir
# This will rewrite if request_filename is not a directory (!-d)
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond is with AND per default
# Only if request + .html extension is a file (-f)
RewriteCond %{REQUEST_FILENAME}.html -f
# If both above conditions are true this rule is followed
# ^(.*)$: Regular Expression Match everything `.*` in the request
# from start `^` to the end `$`
# Brackets mean: capture the text if matched
# $1.html Append .html to the captured text from the Rule
# [L,R=301]: L=Last don't try to rewrite any other rule that comes below
# R=301 Redirect and set the HTTP Status code to 301
RewriteRule ^(.*)$ $1.html [L,R=301]
</IfModule>
Again! This is untested!

If you only need rule for /view-profiles` URI then it will work:
RewriteRule ^view-profiles/?$ view-profiles.html [L,NC,R=301]

Related

Redirect specific URL to another URL on another subdomain

I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem

htaccess - replace part of url

I have a site and i wish to replace a part of URL, https://example.com/[THISPART]/file.ext.
Example URLs that will be requested on site:
https://example.com/example-page/sw.js
https://example.com/example-page/image.jpg
https://example.com/some-other-page/sw.js
https://example.com/some-other-page/file.pdf
https://example.com/page-with-attitude-4/sw.js
https://example.com/page-with-attitude-4/info.txt
How I want to rewrite them:
https://example.com/content/example-page/sw.js
https://example.com/example-page/image.jpg
https://example.com/content/some-other-page/sw.js
https://example.com/some-other-page/file.pdf
https://example.com/content/page-with-attitude-4/sw.js
https://example.com/page-with-attitude-4/info.txt
In other words, if only sw.js is requested, then rewrite to other URL.
What I have used so far in my htaccess is this:
RewriteRule ^(.*)\/sw.js$ content/$1/sw.js [L]
I've been using http://htaccess.mwl.be/ as a tester and test shows alright, but when I use it on site, it doesn't work. Any help?
Put the folowing code at your main directory .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} sw\.js
#the line above to match sw.js
RewriteCond %{THE_REQUEST} !content
# the line above to exclude any request including content from the following rule
RewriteRule ^(.*)$ content/$1 [R=302,L,NE]
#the line above to apply redirection for requests that passed previous conditions and redirect any request ended with sw.js
RewriteRule ^content/(.*)$ /$1 [L,QSA]
#the last line is to make internal redirection for original path
After testing , if it is Ok , change 302 to 301 if you wanna make permanent redirection
Made it!
RewriteRule ^([A-Za-z-]+)/sw\.js$ /places/$1/sw.js [L,QSA]

redirecting to a different directory using htaccess

I'm trying to redirect the following
/books/categories/mountain-literature/my-father-frank.html
to
/books/categories/baton-wicks/my-father-frank.html
This is the line in my .htaccess
RedirectMatch 301 /mountain-literature(.*) /books/categories/baton-wicks$1
it rewrites the url and appends this pageUrl stuff on which I don't want and the correct page doesn't load ?
books/categories/baton-wicks/my-father-frank.html?pageUrl=books/categories/mountain-literature/my-father-frank&contentType=html
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^mountain-literature/(.*)$ /baton-wicks/$1 [L,R=301]
try this or this
Redirect 301 /mountain-literature /baton-wicks
If you dont need the rewrite rule to match multiple URL's, try just hardcoding the target URL, i.e.
Options +FollowSymLinks
RewriteEngine On
RewriteRule mountain-literature/(.*) /books/categories/baton-wicks/my-father-frank.html [L,R=301]
By adding the [L] flag it should tell apache not to allow any other rules to append to the URI

Unable to get .htaccess RewriteRule to work

I've been pulling my hair out trying to get a URL rewrite rule to work using .htaccess. Mod_rewrite is enabled and I have managed to get a 301 redirect to work (from /beta to /Beta/) so I know the .htaccess is able to work.
Basically I'm trying to get /Beta/Page.php?id=page&tab=services&tabid=tab1 to become /page/services (and ideally leave out the tabid if it's not going to break the site removing it).
The code I'm working with currently is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3
redirect 301 /beta http://www.example.com/Beta/
Any help would be gratefully received.
Remove the leading slash:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(Beta)/[^.]+\.php\?id=([^&]+)&tab=([^\s&]*)&tabid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=302,L]
RewriteRule ^Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3 [L,QSA]
This will externally redirect:
/Beta/Page.php?id=page&tab=services&tabid=tab1
to
/Beta/page/services/tab1
and rewrite same URI internally.
Also .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

How to redirect a single web page from one domain to another using .htaccess

How do I get the following redirect to work?
olddomain.com/employee-scheduling-software.html
To redirect to
newdomain.us/employee-scheduling-software.html
I do have mod_rewrite on, but I'm basically a complete novice in this area
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^employee-scheduling-software.html$ http://newdomain.example.org/employee-scheduling-software.html
</IfModule>
You can change the rule into:
RewriteRule ^employee-scheduling-software.html$ http://newdomain.example.org/employee-scheduling-software.html [R=301]
which will send a 301 Moved Permanently header to the browser, so it updates its bookmarks and stuff.
You could use this code in .htaccess on olddomain.com:
RewriteEngine on
RewriteRule ^employee-scheduling-software\.html$ http://newdomain.us/employee-scheduling-software.html [R,QSA]
Since the ^employee-scheduling-software\.html$ is a PERL regex, you need to escape the dot in ".html" with a backslash (\.).
This will just redirect employee-scheduling-software.html to the new domain. If you want to redirect all files to the new domain, use:
RewriteEngine on
RewriteRule ^(.*)$ http://newdomain.us/$1 [R,QSA]

Resources