Rewriterule for any URL ending in specific format - .htaccess

I would like to redirect all URLs ending in
data.html to templates/data.php
info.html to templates/info.php
any_other_file.html to templates/index.php
Again, all URLs ending with those names (could be http://some-domain.com/some/long/path/data.html)

You can use this in your /.htaccess file:
RewriteEngine On
# Internally rewrite data/info.html to the applicable PHP file
# in the templates directory
RewriteRule (data|info).html$ /templates/$1.php [NC,L]
# Rewrite everything else ending in .html to /templates/index.php
RewriteRule ^(.*).html$ /templates/index.php [NC,L]

Related

How to redirect url via .htaccess

I want to redirect via .htaccess
https://www.example.co/en/brand/Abc to https://www.example.co/en/brand/abc
I have tried
RewriteRule ^https://www.example.co/en/brand/Abc https://www.example.co/en/brand/abc [R=301,L]
The RewriteRule pattern (1st argument to the RewriteRule directive) matches against the path-part of the URL only, ie. /en/brand/Abc. An additional complication in per-directory .htaccess files is that the URL-path that is matched is also less the directory prefix (which always starts with a slash), so the URL-path does not start with a slash. In other words: en/brand/Abc (for an .htaccess file in the document root).
So, you will need to format the directive like this instead:
RewriteRule ^en/brand/Abc$ https://www.example.co/en/brand/abc [R=301,L]
(Assuming you already have RewriteEngine On defined and that this is near the top of your .htaccess file.)
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You may try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Don't want loops
RewriteCond %{REQUEST_URI} !/abc
RewriteCond %{REQUEST_URI} /Abc
RewriteRule . https://www.example.co/en/brand/abc [R=301,L]
URL are usually case-sensitive. Check this document, while domain names are not. Therefore "abc" and "Abc" are not the same and that's what the question is about. I think.

Url redirection - changing folder, replacing underscores with dashes and removing html extension

I have an old site where I have underscores and an html extension which I want to redirect in the following way
http://example.com/news/this_is_a_test.html -> http://example.com/post/this-is-a-test
http://example.com/portfolio/another_test.html -> http://example.com/project/another-test
There are other folders apart from news and portfolio and clearly the final segment of the url has an unknown number of underscores.
Here is the .htaccess I am using at the moment (based on my original question htaccess file to remove folder, and replace underscores with dashes). It works for the news example, but breaks if I try for portfolio.
Any idea where I'm going wrong?
RewriteEngine on
# redirect "/news_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]
RewriteRule ^portfolio/(.+)$ /$1 [L,R]
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /project/$1 [L,R]
# remove .html from end of url
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Many thanks!
I had a solution for this before, but it is breaking on my current setup (Apache crashes), and so it would be unwise of me to recommend it for your case. (It could be an issue with my setup, but I'd prefer to give you a more straight-forward route.)
This solution involves sending the relevant requests to a PHP file that will do the necessary replacements, and redirect once only. Note that your current implementation will send multiple redirect instructions to the browser. This is not only bad from a user experience point of view, but also from an SEO one.
To implement the solution, start by replacing your .htaccess directives with this:
RewriteEngine On
# Rewrite news and portfolio links to redirect.php
RewriteRule ^(news|portfolio)/(.+).html /redirect.php [L]
Then, create a redirect.php file in the same directory as your .htaccess file (in this case, your document root), and fill it with this simple replacement method and redirect instruction:
<?php
$path = $_SERVER['REQUEST_URI'];
# Perform the necessary replacements. The first array contains
# what we're searching for, bit by bit, and the second array
# contains the relevant replacements.
$path = str_replace(
['_', '/news/', '/portfolio/', '.html'],
['-', '/post/', '/project/', ''],
$path);
# Now, simply redirect to the new path.
# Change 302 to 301 use a "Moved Permanently" header,
# resulting in browsers and search engines caching
# the redirect.
header("Location: $path", true, 302);

Friendly url pagination

I am new on that htaccess thing.
My problem is, I have a pagination on index.php:
http://localhost/index.php?pagina=2
I want to access this with this url:
http://localhost/page/2
I try on htaccess:
RewriteEngine On
RewriteRule /page/(.*)$ index.php?pagina=$1
but it gave me a 404 error.
In per-directory .htaccess files the directory prefix, of where this .htaccess file is located, is removed from the URL-path when pattern matching. In the document root, this is simply / (a slash). So your RewriteRule would need to be rewritten as:
RewriteRule ^page/(.*) /index.php?pagina=$1 [L]
Use:
RewriteEngine On
RewriteRule page/(.*)$ index.php?pagina=$1
without first /

How to htaccess redirect urls to .html

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]

Redirect hundreds of urls with htaccess

hello my url structure right now for the majority of my links is:
www.url.com/category1/sample-keyword.html
I am looking to redirect them to the new url that has dropped the word sample from the url structure ie to this:
www.url.com/category1/keyword.html
what should i put in htaccess that auto redirects all the urls in the www.url.com/category1/ section to redirect to the new url structure?
This should do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*/)sample-(.*)$ $1$2 [L,R=301]
It will match all URLS with the substring /sample- and strip it from the URL. Depending on your site organization, you may need to adjust the pattern, but that should be a good jumping-off point.
RewriteEngine On
# Redirect sample-*.html to *.html
RewriteRule ^\/?category([0-9]+)\/sample\-([^\/]+)\.html$ http://www.url.com/category$1/$2.html [R=301]
# Serve *.html
RewriteRule ^\/?category([0-9]+)\/([^\/]+)\.html$ page.php?category_id=$1&keyword=$2 [L]

Resources