RewriteRule for first folder in URL only - .htaccess

I have to redirect some folders from my domain to a subdomain. These are my .htaccess rules:
RedirectMatch 301 /folder1 http://sub.domain.com/folder1$1
RedirectMatch 301 /folder2 http://sub.domain.com/folder2$1
All working well, this redirects everything from
www.domain.com/folder1/... => to => sub.domain.com/folder1/...
But what is the world without challenges? I recognized this problem: this rules also redirect
www.domain.com/sub1/sub2/folder1/... => to => sub.domain.com/folder1/...
and in my case this is not good. Any solution to avoid this?
Thanx in advance.

You need to delimite your regex pattern using ^ and $ to match just /folder1 .
RedirectMatch 301 ^/folder1$ http://sub.domain.com/folder1$1

Related

301 redirect all .htm to the root domain

I rebuilt a site and would like to redirect all urls ending in .htm (that might be in folders) to the root of the website.
I don't want to change or remove the extension, just make all the old adresses ending in .htm go to the root website www.website.com/
I tried
RedirectPermanent (.*)\.htm$ https://www.mywebsite.com => does nothing
RedirectMatch (.*)\.htm$ http://www.mywebsite.com => "too many redirects"
You can do this with the RedirectMatch rule.
RedirectMatch 302 \.htm$ /single-page
You can also try a mod_rewrite rule
RewriteRule ^(.*)\.htm$ /single-page [R=301,L]

.htaccess RedirectMatch exclude index.html

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.

Redirect rule in .htaccess

I am trying to create the next rule in the .htaccess, but it does not work and I did not find the error. I want to move on from this:
https://www.miblog.com/2018/02/example-5.html
To
https://www.miblog.com/example-5/
I'm trying with this rule.
RedirectMatch 301 ^/20(.*)/([0-9]{1,2})/([0-9]{1,2})/(.*)(/)?$ /$4
This should do the job ;)
RedirectMatch 301 ^/(20)([0-9]{1,2})/([0-9]{1,2})/(.*)\.html$ /$4/
Or that
RedirectMatch 301 ^/(20)([0-9]{1,2})/([0-9]{1,2})/(.*)\.(.*)$ /$4/
Did you have RewriteEngine on right? :)
Try this :
RedirectMatch 301 ^/(20)([0-9]+)/([0-9]+)/(.*)\.html$ /$4/
Note : clear browser cache then test it .

htaccess redirecting previous URLs to single URL

After deploying a new site, I'm working to redirect old urls to a single page. Trying both redirects and rewrites. Desired:
/oldurl/page1 => /newurl
/oldurl/page2 => /newurl
The following redirect works, but not entirely. For example:
Redirect 301 /oldurl /newurl
Becomes the following, where page1 should actually be removed
/newurl/page1
The following rewrite doesn't rewrite at all:
RewriteRule ^oldurl/(.*)$ /newurl [R=301,NC,L]
You can use RedirectMatch for this to get more control via regex patterns:
RedirectMatch 301 ^/oldurl(/.*)?$ /newurl

RedirectMatch 301 in htaccess file

Can anyone tell me what does
RedirectMatch 301 /([a-zA-Z_]+)-(\d+)-(\d+)-1-([a-z]+)-([a-zA-Z_]+).html http://www.example.com/$1-$2-$3-$4-$5.html
mean in .htaccess file??
This regex is searching for:
[anyChars and_]-[digits]-[digits]-1-[smallChars and_]-[anyChars and_].html
and redirects browser. Ex:
*aaAA-1234-567-1-bbb-cC_c.html*
will be redirected to:
*http://www.example.com/aaAA-1234-567-bbb-cC_c.html*
Also here is a good service to test RegEx: http://gskinner.com/RegExr/

Resources