Magento htaccess redirection issue - php

I was trying to redirect a particular URL and all its child URL's to a particular URL. Using 301 redirection this is working fine but the browser URL is showing the test contents also. Please see below for more details
Example: https://www.mywebsite.com/customer/account/login/referer/* needs to be redirected towards https://www.mywebsite.com/customer/account/login/register
What I tried is:
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /customer/account/login/referer https://mywebsite.com/customer/account/login/register
The htaccess rule is working fine for just this URL. If I type
https://mywebsite.com/customer/account/login/referer/testing
This will be redirected towards https://mywebsite.com/customer/account/login/register/test
Please let me know how I will be able to trim the other part (URL part after register/*)from the redirected URL. What I am trying to achieve is
https://mywebsite.com/customer/account/login/referer/* [ANY URL's AFTER referer/ (Including /referer) needs to be redirected to https://mywebsite.com/customer/account/login/register].

This is expected behaviour, as stated in the documentation of Redirect
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
To redirect all requests starting with this prefix to the given URL, you may use RedirectMatch, which uses regular expressions instead of a URL prefix
RedirectMatch ^/customer/account/login/referer https://mywebsite.com/customer/account/login/register
You don't need to use
Options +FollowSymLinks
RewriteEngine on
because this is for mod_rewrite. Redirect and RedirectMatch are provided by the mod_alias module.

Related

RewriteRule for blog posts with a subdomain in the URL

working on a writing a redirect rule but I'm not too familiar with them or with the syntax.
Basically I have Wordpress URLs that look like this:
https://example.com/blog/blog-title/garbagetext
I need to be able to redirect URLs like this to our 404 page, and my first attempt was with:
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
This worked, but it also made it impossible to access actual blog posts, and is also redirecting URLs that look like this:
example.com/blog/blog-title
Any ideas how to make this work properly?
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
:
example.com/blog/blog-title
The directive you posted could not redirect the example URL that you say is being erroneously redirected. So, either you have other directives (possibly in other .htaccess files, or the server config) or there is a conflict with existing directives, or you are seeing a cached response? If you've previously experimented with 301 redirects then these will have been cached aggressively by the browser. (It can be easier to test with 302 - temporary - redirects for this reason.)
Since you are using WordPress (which uses a front controller) then any redirects should appear at the start of the file, before the front controller.
You also appear to have an erroneous slash at the end of the RewriteRule pattern that will fail to match an intended URL of the form /blog/blog-title/garbagetext.
I'll ignore the backticks for now - I assume they are probably just a styling typo in your question?
I need to be able to redirect URLs like this to our 404 page
You shouldn't be redirecting to a 404 page. It should be served with an internal subrequest instead. If you redirect then you first send a 3xx response back to the client, the client then issues a second request for your 404.php page (exposing the URL) and then you have to make sure you are manually setting the appropriate 404 HTTP response header.
So, you need to define the appropriate ErrorDocument and R=404 the response.
The posts that need to be redirected to the 404 are any posts that have text following that trailing slash, so far example: example.com/blog/customer-reviews/asdfa.
Try the following instead, at the top of your .htaccess file:
ErrorDocument 404 /404.php
RewriteRule ^blog/.+/. - [R=404]
This will match /blog/customer-reviews/asdfa, but it won't match /blog/customer-reviews/ (or /blog/customer-reviews). The R=404 flag triggers an internal request to the defined error document. The L flag is not required when using a none 3xx code. When there is no substitution text (ie. no target URL) then specify a hyphen (-) instead.
And make sure you've cleared your browser cache.

Redirect issue, keeps adding string to url because of wrong rewrite

I have a link: /oldlink/ that needs to be directed to /oldlink/general but if I do this code in my htaccess there is an infinite loop because it keeps adding /general/general/general behind my URL.
Redirect 301 /oldlink/ /oldlink/general/
Is there some kind of code I can add to let it stop adding extra string to the url? Maybe a rewrite instead of a redirect?
Your redirect pattern also matches the destination url. You need to use RedirectMatch instead of Redirect directive to avoid the loop error
RedirectMatch ^/oldlink/?$ /oldlink/general/

Rewrite without changing URL in same domain using htaccess

I have a Two URLs
NEW URL :
http://host.ip.address/nl/business/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
OLD URL :
http://host.ip.address/nl/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
when in request NEW URL from browser it should retrieve content from OLD URL and the URL should not change in browser.
I have tried the below rule in .htaccess it is rewriting and redirecting to the OLD URL which should not do in my case
RewriteEngine on
RewriteRule (^|.*?/)nl/business/(.*)$ /$1nl/$2 [L,R=301]
It is not possible to do a redirect and keep the original URL in the address bar." This is because a redirect, by definition, involves telling the browser to ask for the requested resource at a different URL. So the browser updates its address bar and uses the new URL.
The simplest way would be use a rewrite, not a redirect. An internal rewrite simply serves content from a local file-path that is different from the requested local URL-path.
You can use something easy like this.
RewriteEngine On
RewriteRule nl/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart nl/business/producten-en-diensten/gsm-en-smartphones/samsung-galaxy-j1-2016-zwart
You may need to also add
Options +FollowSymLinks

htaccess redirect not working in same domain

I want to redirect https://xxx.xxxx.com/zzz to https://xxx.xxxx.com/zzz247. I have tried this
Redirect 301 / https://xxx.xxxx.com/zzz247
So now when I try https://xxx.xxxx.com/zzz its take me into https://xxx.xxxx.com/zzz247zzz247zzz247zzz247zzz247zzz247zzz247....
How can I fix this issue? help please
What is required
I need enable only one URL which is https://xxx.xxxx.com/zzz247 even user type https://xxx.xxxx.com/zzz
Instead of Redirect directive use RedirectMatch with regex support:
RedirectMatch 301 ^/zzz/?$ /zzz247
Redirect appends current URI into redirected URI and since you're matching / that will match anything and cause a redirect loop.
Make sure to clear your browser cache before testing.
Using RewriteRule you can do this:
RewriteRule ^zzz/?$ /zzz247? [L,NC,R=301]
? at the end of the target URI is for stripping any query string.

Redirect to a Rewritten URL

OK, so I'm rewriting some page URLs for a custom PHP cart.
I've got the following rules:
RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]
These will allow me to use a url structure like example.com/product/23/product-slug.
That part is working alright. I'm wondering what options I have for the other direction; redirecting requests for the OLD url to the NEW url. So for example, when someone goes to /product.php?id=2 I want to redirect to /products/2/slug.
Any idea how to get this done?
I tried a simple redirect, but would not work:
Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Redirect only takes a url prefix, not a regex (e.g. /store.php or /store)
You need to try RedirectMatch:
RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Also, is it supposed to start with a /? I'm not sure (your RewriteRule entries above start with no slash, for example)
I solved this a different way: with a modification to the store.php file.
I looked at output from print_r($_SERVER) after pinging both the normal and rewritten urls. I found that $_SERVER['SCRIPT_URL'] contains "/store.php" when the normal url is hit and it contains my rewritten path when the rewritten url is hit.
This means I can do a simple test and redirect appropriately:
if ($_SERVER['SCRIPT_URL'] == "/store.php") {
// run some code that will generate the url
$rewrittenURL = generateURL();
// then add:
header("HTTP/1.0 301 Moved Permanently"); // tell spiders this is permanent
header("Location: $rewrittenURL");
}

Categories