Mod_rewrite remove appending URL - apache

I am sure this might have been asked before but I've been pulling my hairs for two days with no luck, what I want to do is do a 301 redirect for www.domain1.com/mydesk/anything to www.domain2.com/blogs. So far what I've been able to do is get the /mydesk requests to go to /blogs on domain2 but when ever something is requested like /mydesk/test1/test it goes through so what I would want is know how to remove the appending URL so anything for /mydesk/anything goes to domain2.com/blogs whatsoever.
so http://www.domain1.com/mydesk/whatever 301 http://www.domain2.com/blogs without the appending URL from the requested link.
Thanks.

If you want everything in the "mydesk" folder to redirect to the same blogs folder on the new domain, you can use this:
RedirectMatch 301 /mydesk(/.*)? http://www.example.com/blogs
If you want to append whatever is after "mydesk" to the new URL (e.g. /mydesk/some-specific-blog -> example.com/blogs/some-specific-blog), you can just append a $1 to the end of the line, like so:
RedirectMatch 301 /mydesk(/.*)? http://www.example.com/blogs$1

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

Redirect and append GET-Parameter

I'm having trouble with redirecting from one URL to another, while appending one GET-parameter to the NEW URL.
The first time (from root of old domain to root of new domain) works perfectly fine. After (further down in the .htaccess this one case) the GET-parameter appears randomly in the middle of the new url.
Obviously that leads to some nasty 404 situations...
RewiriteEngine on
Redirect 301 / https://foo.bar?redirect=1
Redirect 301 /foo/bar/ https://foo.bar/foo/bar?redirect=1
To give further information:
The idea is to redirect inklusive this parameter, to trigger a popup, giving information about the recent redirect so the user doesn't lose his confidence about the visit of foo.bar.
The first redirect will result in just the right way, it works perfectly.
The second thou, turns out like:
https://foo.bar/foo/?redirect=1bar/
Please and Thank you :)
Your rules work fine as you configure them , why ?
With mod_alias redirect and this part
Redirect 301 / https://foo.bar?redirect=1
Will match / & /foo/ & /foo/bar/ and so on , so the second rule will not work at all because of every request being captured by firrt rule ,and the result of redirecting /foo/bar/ with first rule is foo/bar?redirect=1foo/bar/ because with redirect every things after match will be appending to new target.
To avoid that use RedirectMatch
RewriteEngine On
RedirectMatch 301 /?$ https://foo.bar/foo/bar?redirect=1
RedirectMatch 301 /foo/bar/ https://foo.bar/foo/bar?redirect=1
By this way with regex , you will be able to match against root only by first rule then against /foo/bar/ by second rule.
Note: clear browser cache then test

htaccess redirecting website.com/folder/content to subdomain.website.com/content

I recently moved a Wordpress blog from website.com/wordpress to help.website.com. Now I want to redirect the links to the old adress to the new adress.
I deleted everything from the /wordpress folder except the .htaccess. This file has the following code:
RedirectMatch 301 (.*) http://www.help.website.com$1
It redirects my old links but I don't know how to remove the /wordpress from them.
For example if I access website.com/wordpress/article-categories/example-article/ it sends me to help.website.com/wordpress/article-categories/example-article/, but I want to get to help.website.com/article-categories/example-article/ instead. How do I achieve this?
Try
RedirectMatch 301 ^/wordpress/(.*)$ http://www.help.website.com/$1
This will match the folder literally without capturing it.

Trying to redirect anything after the full URL without /68 for example

I need to remove anything on the end of URLs like the below examples without the /68 or any number ID
https://www.website.com/forum/making-coffee/68 <-- I would like to remove the / and anything after so it looks like this below.
https://www.website.com/forum/making-coffee
I've searched but can't get the redirect to work correctly.
Any idea?
You can use this rule in your root .htaccess
RedirectMatch 301 ^/(.+?)/[0-9]+$ /$1

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.