301 Redirect A URL Pattern Using .htaccess - apache

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.

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

Rewrite language code into url if missing

I want to change an existing Magento store to add store/lang codes to the url i.e.
http://mystore/en/PRODUCTXYZ.html
http://mystore/de/PRODUCTXYZ.html
Old links to http://mystore/PRODUCTXYZ.html will now throw a 404 error.
How can I create an Apache url rewrite rule to add a language code if it is missing i.e. rewrite
http://mystore/PRODUCTXYZ.html
to
http://mystore/de/PRODUCTXYZ.html
So that old links 301 redirect to the correct product.
I have worked around this with
Redirect 301 /PRODUCTXYZ http://mystore/de/PRODUCTXYZ.html
But obviously for thousands of products this might not be practical.
You can redirect multiple Product.html urls with just 1 line of code using RedirectMatch :
RedirectMatch 302 ^/([^/.]+)\.html$ http://example.com/de/$1.html
I used 302 for testing purposes and to avoid browser's cache,
change 302 to 301 (permanent redirect) when you are sure the redirect is working.

Apache rewriterule to redirect folder to subfolder without loop

Long story short, I am trying for the last 3 hours to perform what seems to be a basic url redirect. All the website requests to mysite.com/folder should be redirected to mysite.com/folder/subfolder/. The redirect should be performed "only!" if the user enters /folder. If he accesses /folder/file no redirect should happen.
I have tried the following but with no success, all attempts cause an infinite loop:
RewriteRule ^/folder$ /folder/subfolder/ [R]
Redirect 301 /folder /folder/subfolder/
I don't have access to the vhost file, neither am I allowed to perform this using php or a similar 'workaround'. Either way, htaccess should be the best place to do this. Can anybody shade a light on what I am doing wrong?
redirect use a prefix pattern matching, so instead you should use RedirectMatch with a regex, so that you will match only the directory access and not the file access in the subdirectory. You do not need the rewriteRule, which is doing the same thing.
This should work:
RedirectMatch ^/folder/$ /folder/subfolder/
Be careful, you used a 301 Redirect, 301 means permanent, so you may need to close your browser before testing the new rule, your browser has memorized the redirect permanent answer and will not ask it again until you close it.

Mod_rewrite remove appending URL

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