Page Redirect 301 using htaccess - .htaccess

I want to redirect /event/path1/{id}/{some_name} to /event/path2/{id},
I want to redirect old page to the new one and remove the last segment of the url,
I have done with this but not removing the last segment of the url, this is what .htaccess
RewriteRule ^event/path1/(.*) /event/path2/$1 [L,R=301]

You're using .* in your regex that matches everything after /event/path1/ thus resulting in wrong target URL.
You may use:
RewriteRule ^event/path1/([^/]+)/[^/]+/?$ /event/path2/$1? [L,R=301,NC]
Here [^/]+ will match 1 or more of any non-slash character.
? at the end of the target will strip off any previous query string.

Related

remove all querystring from any url [duplicate]

Have been trying to write a redirect rule with query string but did not succeed.
I have the URL example.com/blog/?page=1 or example.com/blog/?hello, so it does not really matter what goes in the query string. How do I write a Redirect rule, so that it cuts the query string and redirects to the URL before the query string. For example, both of those URLs have to redirect to example.com/blog/ so that URL does not contain any query string.
I was trying
RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.
Also tried
RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got the message that page is not working, 'URL' redirected you too many times.
BTW, technology I am using is Gatsby with htaccess plugin.
To remove the query string you first need to check that there is a query string to remove, otherwise, it should do nothing.
For example, to remove the query string from /blog/?<query-string> you would do something like this:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(blog)/?$ /$1/ [QSD,R=302,L]
This matches the URL-path blog/ (trailing slash optional) and redirects to /blog/ (with a trailing slash). Your example URL includes the trailing slash, but your regex appears to suggest the trailing slash is optional?
The preceding condition (RewriteCond directive) checks the QUERY_STRING server variable to make sure this is non-empty (ie. it matches a single character, denoted by the dot).
The $1 backreference in the substitution string contains the value from the captured group in the preceding RewriteRule pattern. ie. "blog" in this example. This simply saves repetition. You could just as easily write RewriteRule ^blog/?$ /blog/ [QSD,R,L] instead.
The QSD (Query String Discard) flag removes the original query string from the redirected response, otherwise, this would be passed through by default (which would create a redirect-loop).
If the request does not contain a query string then this rule does nothing (since the condition will fail).
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent), but only once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.
A look at your existing rules:
was trying RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.
By default, the relative substitution string (ie. blog/) is seen as relative to the directory that contains the .htaccess file and this "directory-prefix" is then prefixed back to the relative URL, so this will (by default) result in a malformed redirect of the form https://example.com/path/to/public_html/blog/.
Also tried RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got message that page is not working, 'url' redirected you too many times.
This is not checking for (or removing) the query string so this is basically just redirecting to itself - an endless redirect-loop.
Remove any query string from any URL
What rule do i write, to remove query string from any URL.
Modify the RewriteRule pattern to match any URL and redirect to the same. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1 [QSD,R=302,L]
This needs to go at the top of the root .htaccess file before any existing rewrites.
If the .htaccess file is in a subdirectory (not the root) then you will need to do something like the following instead, since the $1 backreference (as used above) won't contain the complete root-relative URL-path.
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=302,L]

Redirect rule to remove query string from URL

Have been trying to write a redirect rule with query string but did not succeed.
I have the URL example.com/blog/?page=1 or example.com/blog/?hello, so it does not really matter what goes in the query string. How do I write a Redirect rule, so that it cuts the query string and redirects to the URL before the query string. For example, both of those URLs have to redirect to example.com/blog/ so that URL does not contain any query string.
I was trying
RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.
Also tried
RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got the message that page is not working, 'URL' redirected you too many times.
BTW, technology I am using is Gatsby with htaccess plugin.
To remove the query string you first need to check that there is a query string to remove, otherwise, it should do nothing.
For example, to remove the query string from /blog/?<query-string> you would do something like this:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(blog)/?$ /$1/ [QSD,R=302,L]
This matches the URL-path blog/ (trailing slash optional) and redirects to /blog/ (with a trailing slash). Your example URL includes the trailing slash, but your regex appears to suggest the trailing slash is optional?
The preceding condition (RewriteCond directive) checks the QUERY_STRING server variable to make sure this is non-empty (ie. it matches a single character, denoted by the dot).
The $1 backreference in the substitution string contains the value from the captured group in the preceding RewriteRule pattern. ie. "blog" in this example. This simply saves repetition. You could just as easily write RewriteRule ^blog/?$ /blog/ [QSD,R,L] instead.
The QSD (Query String Discard) flag removes the original query string from the redirected response, otherwise, this would be passed through by default (which would create a redirect-loop).
If the request does not contain a query string then this rule does nothing (since the condition will fail).
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent), but only once you have confirmed this works as intended. 301s are cached persistently by the browser so can make testing problematic.
A look at your existing rules:
was trying RewriteRule ^blog/?$ blog/ [R=301,L,NE] but got redirected to 404 page.
By default, the relative substitution string (ie. blog/) is seen as relative to the directory that contains the .htaccess file and this "directory-prefix" is then prefixed back to the relative URL, so this will (by default) result in a malformed redirect of the form https://example.com/path/to/public_html/blog/.
Also tried RewriteRule ^blog/?$ /blog/ [R=301,L,NE] and got message that page is not working, 'url' redirected you too many times.
This is not checking for (or removing) the query string so this is basically just redirecting to itself - an endless redirect-loop.
Remove any query string from any URL
What rule do i write, to remove query string from any URL.
Modify the RewriteRule pattern to match any URL and redirect to the same. For example:
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1 [QSD,R=302,L]
This needs to go at the top of the root .htaccess file before any existing rewrites.
If the .htaccess file is in a subdirectory (not the root) then you will need to do something like the following instead, since the $1 backreference (as used above) won't contain the complete root-relative URL-path.
RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI} [QSD,R=302,L]

how to make a permanent redirect via htaccess?

Google has indexed some URLs with empty value - space - at the end of each URL, I want to make a permanent redirect via .htaccess here is my code but it did not redirecting :
RewriteRule ^/profile/userx/([a-zA-Z])+/[a-zA-Z]+/[a-zA-Z]/'%20'$ https://mywebsite.net/profile/userx/([a-zA-Z]+)/[a-zA-Z]+/[a-zA-Z]/ [R=301,L]
How to say any URL with space at the end redirect it to that one without space?
You may use this rule as your topmost rule to remove 1+ whitespace from the end of URL:
RewriteEngine On
RewriteRule ^(.*)\s+$ /$1 [L,NE,R=301]
# other rules go below this line
\s+$ matches 1+ whitespace at the end of a URI and .* matches anything before whitespaces.

How to exclude trailing slash on source URL when redirecting?

I have to make a 301 redirect from a domain to another (example-a.com to example-b.com).
This is the code I put in .htaccess of example-a.com:
RewriteEngine on
RewriteRule ^(.*)$ https://www.example-b.com/$1 [R=301,L]
All URLs are the same, except pages of example-a.com all have a / (slash) at the end of the URL, but example-b.com does not. So how to redirect without the ending / if the URL contains one?
RewriteRule ^(.*)$ https://www.example-b.com/$1 [R=301,L]
To exclude the slash from the redirected URL, then exclude the slash from the captured group in the RewriteRule pattern. eg. ^(.*)/$.
However, assuming you also want the document root to redirect, where there is no slash in the URL-path, then you need to make the trailing slash optional (or create an entirely separate rule). But in that case you need to also make the capturing group non-greedy, otherwise the trailing slash will always be included in the captured group (since regex is greedy by default).
So, try the following instead:
RewriteRule ^(.*?)/?$ https://www.example-b.com/$1 [R=302,L]
You will need to clear your browser cache before testing. Since the earlier 301s will have likely been cached.
Note that this is a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect - if that is the intention - once you have confirmed that it works OK. This is to avoid the browser caching erroneous redirects whilst testing.

Not able to redirect the URL contain %00

I want to redirect a URL which contains symbols and numbers, characters to some other URL and... URL which contains %00 at the end needs to redirect to another URL.
Example:
www.example.com/asdnsadnas%00 redirect to another URL.
%00 is not being accepted in redirect URL, please help me out.
How you handle this really depends on what other "symbols and numbers" you are referring to and where in the URL these occur.
Since the RewriteRule pattern matches against the %-decoded URL-path and %00 is NULL then to catch %00 in the URL-path you can try matching against THE_REQUEST, which contains the raw first line of the request (which is not %-decoded).
www.example.com/asdnsadnas%00 redirect to another URL.
For example, try the following near the top of your root .htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /asdnsadnas%00\ HTTP
RewriteRule ^asdnsadnas /another-url [R,L]
The RewriteRule pattern just checks the first part of the URL, up to the %00.
If you just wanted to redirect /asdnsadnas and simply ignore everything that follows that URL-path then you don't need to explicitly check for %00, so you can remove the RewriteCond directive.
www.example.com.nz/XYZABC%00 needs to redirects to www.example.com.nz/insurance-hub-page/xsserror/ XYZABC - any letter or symbols etc but I need any URL at the end %00 need to redirect to another URL.
(I assume that space in the target URL is just typo?)
In this case you don't necessarily need to match %00 (a "character" that you don't want). Just be specific about the characters you do want to match (which is likely to be a smaller subset).
For example, the following would redirect /XYZABC to /insurance-hub-page/xsserror/XYZABC. The trailing %00 is ignored.
RewriteRule ^(\w+) /insurance-hub-page/xsserror/$1 [R,L]
...but I need any URL at the end %00 need to redirect to another URL.
Not sure what you mean by that?
if URL contains any special charcters or symbols etc need to redirects to another url Example: www.example.com/%28dsajkd%20nkasd%20daskdasj%00
You could just check to see if the requested URL contains any %-encoded characters, which seems to fit your example:
RewriteCond %{THE_REQUEST} %
RewriteRule ^ /another-url [R,L]

Resources