Issue with .htaccess redirects - apache

I am trying to run a bunch of 301 redirects using an .htaccess file. However my rules are not working. I'm not sure what I'm doing wrong.
Here is my rules:
RedirectMatch 301 /directory/1 http://www.example.org/newdirectory
RedirectMatch 301 /anotherdirectory/2/3 http://www.example.org/newdirectory
If I visit www.example.org/directory I get redirected to the correct place. However if I visit www.example.org/directory/1 I do not get redirected. It seems to only work when there is just 1 level if I try to add more levels it stops working
Is there an issue with using "/" in the match? Should I try a regex expression instead of the actual path?
Please anyone can shed some light on this?

Related

Prevent wildcard in htaccess 301 redirect

I have setup a htaccess 301 redirect to redirect an indexed page to a new page, however this is also acting as a wildcard redirect for child pages which I do no what to happen.
Old structure
example.com/faq
example.com/faq/question-1
example.com/faq/question-2
etc etc
New structure
example.com/faqs
example.com/faq/question-1
example.com/faq/question-2
etc etc
htaccess redirect in place :
Redirect 301 /faq/ https://example.com/faqs/
This is working with no issues to send /faq to /faqs however it is also sending /faq/* to /faqs/* which I do not want to happen.
For example going to example.com/faq/question-1 causes a to many redirects error and finally lands on example.com/faqs/question-1
Is there anything i can add to the single redirect line to prevent this happening, or is their a more complex use of RewriteRule I could use instead. Research into the matter initially seem to confirm that this should/would happen, and if it does what can be added to prevent it. After a prompt to the apache docs I can see why they would redirect as a wildcard.
After suggests from CBroe an implementation of using RedirectMatch worked.
RedirectMatch 301 ^/faq/$ https://example.com/faqs
This now redirects /faq to /faqs, however doesn't redirect /faq/question-1 to /faqs/question-1 etc

Apache htaccess 301 Redirect to subfolder causes too many redirects

Goal
On an Apache httpd server, I would like to redirect requests made to /cms/de/foo to /cms/de/foo/bar permanently.
Failed attempt
Trying the following redirect in the .htaccess
Redirect 301 /cms/de/foo /cms/de/foo/bar
results in too many redirects.
Request: http://threeohone.localhost/cms/de/foo
Redirect: http://threeohone.localhost/cms/de/foo/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar/bar
As far as I understand it, the redirect is applied over and over again until it results in said error. An exact match could solve it, I believe, but I am unsure on how to accomplish it.
Language part
de represents the language and can be any of de, en and fr. Would it make sense to cover all languages within the same redirect or have explicit redirects for each language? The foo respectivley foo/bar path is the same for all languages.
Thank you #CBRoe for pointing out RedirectMatch. This works:
RedirectMatch 301 ^/cms/de/foo$ /cms/de/foo/bar

Redirect sub.domain.cz/xxx to sub.domain.cz

I would like to redirect anything after the slash to version without slash.
That means if somebody writes
subdomain.domain.cz/420 (or any number)
it will redirect to
subdomain.domain.cz/
I tried to create .htacess file with
RewriteEngine On
RewriteRule subdomain.domain.cz/?(.*)$ http://subdomain.domain.cz/$
However it doesnt work. I tried to modify it somehow but it always show some error like i have to many redirects.
Can I ask you for some help please? It's my first time working with .htaccess
Thank you!

301 Redirect A URL Pattern Using .htaccess

I want to redirect my URLs to a new pattern. For this purpose, I used 301 redirect for every single URL but that are taking a huge time and my .htaccess file is going large and large as I have thousands of URLs.
So now Someone said to me to use .htaccess to use 301 redirect or rewrite engine option. Now I am new to .htaccess to use Pattern Redirect. First of all clear me out that is this possible to use 301 redirect in patterns? If yes then Can I do pattern 301 redirect in the below URLs? I want to redirect the below pattern so can you help me?
/search/label/XXXXXXXXXX to /category/XXXXXXXXXX
/year/month/XXXXXXXXXX.html/?m=0 to /year/month/XXXXXXXXXX.html
/year/month/XXXXXXXXXX.html/?m=1 to /year/month/XXXXXXXXXX.html
/search to /
/feed to /
XXXXXXXXXX means some text/no that are dynamic and changeable. year and month means only no that are also dynamic and changeable. / means site homepage. Rest are fixed text.
Please keep in mind that sometime there are many variables in every URL so we also want to avoid that that always start from ?variable=value&variable=value in the end of every URL.
After asking here, I keep trying myself too so I am able to do it and working on my side. I added below codes in my .htaccess file and after that I am able to redirect all upper URLs without any 404 error.
Redirect 301 /search/label http://www.example.com/category
Redirect 301 /search http://www.example.com
Redirect 301 /feed http://www.example.com
For 2,3 URL pattern, I did nothing because after checking, its not showing any 404 error as they are only variable in front of URL so no need to edit that.

htaccess rewrite rules are not working with urls that end with .cfm

I'm working on fixing all my URL's to be shorter with 301 redirects. I have fix almost all of them, however there is a url that is ending with .cfm that will not rewrite.
FROM: http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
TO: http://www.mydomain.com/story/resources/health/page/168/page.html
If I change /page.cfm to /page.html then the rewrite will work.
Here is the rewrite rule that works for my other urls
RewriteRule ^index.cfm/catlink/([a-zA-Z0-9/-]+)([/])pagelink/([a-zA-Z0-9/-]+)([/])sublink/([a-zA-Z0-9/-]+)([/])art/([a-zA-Z0-9/-]+)(.*)$
http://localhost/index.cfm?page=moved&cat=$3&subcat=$5&article=$7&story=$8 [R=301]
Why does it work when the URL ends with .html but not when it ends with .cfm? What am I doing wrong?
This is current link and will not work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.cfm
If I manually change the end of it to .html, I can get it to work:
http://www.mydomain.com/index.cfm/catlink/17/pagelink/7/sublink/34/art/41/rec/1/page.html
The issue is that Apache httpd is passing it off to Tomcat before Apache looks at the .htaccess. To test this, move your rewrite rules into your vhost. If they work, then that's what the problem was.
First off, change your the first part of your RewriteRule to be the following, more concise expression:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/(.*)$
I believe that alone might resolve the issue. However, if it does not, and you don't care about the rest of the URL, try the following:
^index.cfm/catlink/(\d+)/pagelink/(\d+)/sublink/(\d+)/art/(\d+)/
Note: this removes the anchor ($) and therefore allows the URL to be open ended.