.htaccess redirect to different domain only if URL is invalid - .htaccess

I am trying to create a .htaccess rule to redirect users from one domain to the other, but this should happen only if the original URL does not exist. For example:
www.domain.com/100 to www.otherdomain.com/PersonalPages/100
www.domain.com/150 to www.otherdomain.com/PersonalPages/150
Now, I don't have a page/file named 100 or 150, so that's why it should redirect, but if user enters "www.domain.com" I do have "index.php" and should stay in "www.domain.com" and not try to go to "www.otherdomain.com/PersonalPages/index.php" (or any other if page exist in my www.domain.com)
I know I can create a simple 301 redirect, but the numbers in the URL are the IDs of users and that number will grow on a daily basis so I can't be adding lines of redirects to my .htaccess for each new user.
Is there a 404 code I could use?

I think this will work for you:
RewriteRule ^([0-9]+)$ http://www.otherdomain.com/PersonalPages/$1
^ and $ mark the beginning/end of the url, [0-9]+ stands for one or more numbers between 0 and 9.

Related

Changing Domain: 301 Redirect for multi language E-Commerce site

I am moving my WP ecommerce site to a new domain and I need to code a more advanced htaccess 301 redirect to pass on the SEO love. (I say htaccess but maybe there is a server side way that is better)
I have made sure, as much as possible, to keep the URL structure the same so for the products/posts, categories, tags and most pages everything after the olddomain.com/XXXXX is the same.
However, I don't want to do a blanket redirect for everything because there are some parts of the site that will not match so I thought it better to break into into chunks/functions.
(Maybe this is a bad strategy and I should just do do one blanket redirect and the trouble shoot page not found as it all goes live?)
redirect function for products
redirect function for categories
redirect function for tags
individual redirects for the rest
There are also three languages with sub folder /ca/ and /es/ - example.com/es/products - assuming I can just copy the function for each language and appending the subfolder.
Examples:
oldomain.com/product/any-product-ABC
redirect to
newdomain.com/product/any-product-ABC
(Domain change) (folder same) (product added from previous)
Then same redirect for languages
oldomain.com/es/product/any-product-ABC
redirect to
newomain.com/es/product/any-product-ABC
How do I write the above redirects?
I say htaccess but maybe there is a server side way that is better
If you have access to the Apache server config then you can indeed simplify these redirects, which will also be more efficient since it will reduce the load (if any) from the main site.
Create a separate <VirtualHost> container for the old domain and then you can use simpler mod_alias directives.
For example, for the two examples you gave. eg. /product/<product-name> or /<lang-code>/product/<product-name> to the same URL at the new domain then you can use following single rule:
RedirectMatch 301 ^(/[a-z]{2})?/product/[\w-]+$ https://newdomain.com/$0
This matches any optional 2-character language code. If you only have three languages then you could be more specific and use regex alternation instead, eg. (ca|es|id) in place of [a-z]{2}. The <product-name> is assumed to consist of the following characters only: a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
However, if you are restricted to .htaccess and both domains resolve to the same place then you will need to use mod_rewrite and check the requested hostname. For example, the following would need to go near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com [NC]
RewriteRule ^((ca|es|id)/)?product/[\w-]+$ https://newdomain.com/$0 [R=301,L]
Note the regex is slightly different to the mod_alias RedirectMatch directive used above since the RewriteRule pattern (first argument) matches against the URL-path less the slash prefix.
Do not repeat the RewriteEngine directive if it already occurs elsewhere in the config file. The order of the directives is important. The above redirect must go before any existing internal rewrites and ideally before any canonical redirects (to minimise the number of redirects).
You should first test with 302 (temporary) redirects to avoid potential caching issues.
so I thought it better to break into into chunks/functions.
That's fair enough. Although you need to make sure that any URLs that you don't redirect from the old domain return a 404 or 410.
And it may be possible to combine "products", "categories" and "tags" into a single rule, depending on exactly the format of these URLs.

How to make 301 redirect from one website to another where the ending is different?

I have 2 websites:
OLD one - https://www.old.example/en/
NEW one - https://new.example/en
Lastly, Google Search Console reported around 80 improperly redirected links for OLD website, i.e.:
https://www.old.example/en/?p=41310
https://www.old.example/en/?p=45659
https://www.old.example/en/?p=72785
In .htaccess of OLD page is inputted only code:
Redirect 301 / https://new.example/
which redirects above links from OLD page to i.e.
https://new.example/en/?p=62692
How can I correct it and i.e. expect to have in such cases always redirection to main page - https://new.example/en
To remove the query string completely (without a stray ? at the end) you'll need to use mod_rewrite instead.
For example, in the .htaccess at the old domain:
RewriteEngine On
RewriteRule ^(en)/ https://new.example/$1 [QSD,R=301,L]
Aside: Although this many-to-one redirect will likely be seen as a soft-404 by Google.

Redirect 301 via htaccess with wildcard

I hope I am asking this questions correctly, if I am not, please feel free to correct me.
I am trying to redirect 301 via htaccess file for all user profiles within the same site so for example. The first section shows the actual URLs I want to redirect from and to where, but this is only focused on 1 user account.
Redirect 301 /otsn/members/admin/my-orders/ /otsn/members/admin/shop/
I am thinking can I use the percentage symbol to make this redirect universal to all users, maybe I'm wrong, Can I do the following?
Redirect 301 /otsn/members/%/my-orders/ /otsn/members/%/shop/
I created a better user profile shop tab that has more capabilities, so I want to get rid of the old one and in case anyone tries to enter the old version of that profile page tab by entering the url manually, I want them sent to the new version for their profile shop page but I want this to happen with every user profile in the system.
Is this the correct method? or is there a better more efficient way of doing this?
Thank you
You may use this rule as topmost rule in your .htaccess or Apache config:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([\w-]+)/members/([\w-]+)/my-orders/?$ [NC]
RewriteRule ^ /%1/members/%2/shop [L,R=301]
%1 is back-reference for the first value we are capturing in RewriteCond i.e. first pattern in (...) and same way %2 will represent second captured value in (...).

how to redirect URL containing query string and random numbers in .htaccess

When customers cancel a transaction on my site, they get redirected to the WooCommerce cart page with a query string containing randomly generated numbers at the end.
Example
https://www.example.com/cart/?woo-paypal-cancel=true&token=EC-5474842406066680S
(I need this redirect due to a plugin conflict between WP Rocket cache with CDN activated and WooCommerce. Long story.)
I'm wondering what exactly I would put in my .htaccess file to get it to redirect to
https://www.example.com/cart/
I've tried a number of variations I found on multiple pages here on Stackpath, but it wasn't redirecting. Obviously I'm missing something so I'm turning to the gurus.
Would be very grateful for your help.
To redirect /cart/?woo-paypal-cancel=true&token=<anything> to /cart/ you can try something like the following near the top of your .htaccess file (using mod_rewrite):
RewriteEngine On
RewriteCond %{QUERY_STRING} ^woo-paypal-cancel=true&token=
RewriteRule ^cart/$ /cart/? [R,L]
The ? on the end of the RewriteRule substitution strips the query string from the request.
This is a temporary (302) redirect.

How to “redirect” from main page in Prestashop to specific product page?

This question is the same one that was asked on Feb 24.
I applied the answer using my site product page which is:
Redirect 301 / http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
The error message I receive is WEBPAGE NOT AVAILABLE
The URL line contains:
http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.htmlhome/8-the-lifter-scrollsaw-arm-lift-assembly.html (and all of this text- except for the web address - repeats many many times on the URL line.
Try using RedirectMatch instead because Redirect links two different path nodes together. Meaning, / gets symbolically linked to http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
. Thus anything after the / also appears at the end of http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html.
Incidentally, that means you're browser will continue to loop and constantly get redirected by the server. So try:
RedirectMatch 301 ^/$ http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
You need to create a .htaccess file in the /home subdirectory with the following line:
RewriteEngine On

Resources