Recreate a file which was onece 301 redirected - apache

If I have a file named /a.php, and with .htaccess I redirected that URL to /b.php with 301 flag. For the SEO sake, I have to use 301 flag. But what if I need to get back to /a.php ??? Browser which once faced that 301 redirect will remember this and do redirect until the remove cookies themselves. So What should be my plan???

But what if I need to get back to /a.php ???
Then you remove the 301 redirect to the b.php file and create a new 301 redirect to the a.php file when the b.php file is requested. Even if browsers remember that a->b, when the browser requests b.php, it'll be 301 redirected back to a. As long as the old 301 redirect isn't there, the browser will then try to load a.php.

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

HTACCESS redirection

I changed the hierarchy of my site. there we 2 pages as following:
http://www.example.com/cms/products-search.php
Which moved to : /new/pro-search.php
And
http://www.example.com/cms/data-products-details.php?pro=1111
Which moved to /new/products-details.php?pro=1111
How can i handle this in htaccess and not affect SEO ?
You should redirect to your new page location using
301 "Moved Permanently" redirect
Which has to be setup pro grammatically in your HTACCESS file, simple example:
Redirect 301 /something https://www.mynewapp.com/something/a?someextra
The 301 "Perma Redirect" tell Search Engine that you page has moved for good at new location and any Page Rank should be transferred to new URL.

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.

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.

301 redirect with {~`% characters in url

Trying to apply a 301 redirect to this url "/Qx%40w%2C6M42VAwp3%40Rb%7B~cC4ure%60QWI9" but it's not liking it, i'm guessing because of the horrible url and all the strange characters in it, any ideas?
This is the code I currently have:
redirect 302 /Qx%40w%2C6M42VAwp3%40Rb%7B~cC4ure%60QWI9 /product-tool-case.php
You can do this with mod_rewrite as follows:
RewriteRule ^/Qx%40w%2C6M42VAwp3%40Rb%7B~cC4ure%60QWI9 /product-tool-case.php
I?f you want to continue using mod_alias as above, then you should ensure that the old url is url-decoded. So in the above example try:
redirect 302 /Qx#w,6M42VAwp3#Rb{~cC4ure`QWI9 /product-tool-case.php