.htaccess RedirectMatch exclude index.html - .htaccess

I have in .htaccess a redirect that should transform the url with .html in without extension which works great.
RedirectMatch 301 ^/([^/]+)/([^/]+)\.html$ /$1/$2/
I want to exclude from the rule if the filename it's named index.html how do I do that?
What I tried was
RedirectMatch 301 ^/([^/]+)/(?!index|[^/]+)\.html$ /$1/$2/
But now it isn't working at all.

You may use this regex in your rule:
RedirectMatch 301 ^/([^/]+)/(?!index)([^/]+)\.html$ /$1/$2/
Your attempts at negative lookahead doesn't seem to be correct because of | in expression.

Related

How can I redirect several different files to an url using .htaccess?

I want to edit .htaccess placing the lines below into a single line:
Redirect 301 "/smartphone" "/roster.php"
Redirect 301 "/mobile" "/roster.php"
How can I do that?
The corresponding RedirectMatch directive allows you to use a regex. For example:
RedirectMatch 301 ^/(smartphone|mobile) /roster.php
Reference:
https://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch

Change Directory with Wildcard Redirect using RedirectMatch not working

I am trying to use a RedirectMatch to redirect all files under one directory to the main directory:
RedirectMatch 301 ^/question/.*$ https://www.sample.com/
So that if I have a URL like www.sample.com/question/what-day-is-it
It will automatically redirect to www.sample.com/what-day-is-it
The problem is when I put in the RedirectMatch like above, it redirects all URLs to the homepage: www.sample.com/question/what-day-is-it to www.sample.com
If anyone else had the same problem, a simple Redirect 301 /question / did the trick.

Need to redirect On /clinics directory by unique word

I need redirect url like http://www.sportsinjuryclinic.net/clinics/england/bedfordshire/luton to http://www.sportsinjuryclinic.net/clinics can anyone give me a better solution to redirect..There is 1000's url like this.
Thanks,
You can use this RedirectMatch rule in your root .htaccess:
RedirectMatch 301 ^/(clinics)/.+$ /$1
How can i redirect it to clinics/CA/england/ to the /clinics
RedirectMatch 301 ^/(clinics)/CA/england/ /$1

.htaccess RedirectMatch redirects all pages like "something"

I want to redirect
mysite.com/something
to
othersite.com/someone
I am using this (in htaccess):
RedirectMatch 301 /something http://othersite.com/someone
but it redirects all pages like mysite.com/ksadkahsdhasdhkas/something and I want to redirect only mysite.com/something
How to do it?
Use regex anchors ^ and $ to make sure you don't match any unwanted URI:
RedirectMatch 301 ^/something/?$ http://othersite.com/someone

Redirect directory to the same directory but in a subfolder using htaccess

Lets take the following example:
Redirect 301 /crowdfund/ http://website.com/crowdfund/browse/
Currently its creating a redirect in this format:
http://website.com/crowdfund/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/browse/
What am i doing wrong here?
Thanks!
Use RedirectMatch for exact regex match instead:
RedirectMatch 301 ^/crowdfund/?$ http://website.com/crowdfund/browse/
In your rule /crowdfund/ is being matched before and after redirect.

Resources