I'm having a problem making a 301 redirection.
My old URL is
https://example.com/es/component/users/registration?item=21
The new one is
https://example.com/es/login/registro
So, I wrote this in .htaccess:
RewriteRule ^es\/component\/users\/registration\/?item=21$ "https://mywebsite.com/es/login/registro" [R=301,L]
But it redirects to https://mywebsite.com/es/login/registro?item=21
how can I get rid of this ?item=21 string too?
The directive you posted wouldn't actually perform the redirect as stated, unless the ? was URL encoded (as %3F) in the request and there is no actual query string? Or you are seeing a cached response from an earlier (erroneous) attempt? (301 redirects will be cached persistently by the browser.)
The RewriteRule pattern matches against the URL-path only, not the query string. To match against the query string you need an additional RewriteCond directive and check against the QUERY_STRING server variable.
Try the following instead:
RewriteCond %{QUERY_STRING} ^item=21$
RewriteRule ^es/component/users/registration$ /es/login/registro [QSD,R=302,L]
The QSD (Query String Discard) flag (Apache 2.4+) removes the query string from the redirect response. Otherwise, the default action is to append the query string from the request. If you are using Apache 2.2 then you will need to append a ? to the end of the substitution string instead (essentially appending an empty query string).
No need to backslash escape the slashes in the RewriteRule pattern.
Test first with a 302 (temporary) redirect before changing to a 301 (if that is the intention) when you are sure it's working OK in order to avoid potential caching issues.
You will need to clear your browser cache before testing.
Related
How can I redirect a URL that has a "?" in the middle of it (not like a typical query string)?
The problem URL:
https://example.com/michigan/jobs/?/jobs/
I need to rewrite or redirect it to:
https://example.com/michigan/jobs/
I have tried all of these, and none of them work (no change):
1. Redirect 301 https://example.com/michigan/jobs/?/jobs/ https://example.com/michigan/jobs/
2. Redirect 301 /michigan/jobs/?/jobs/ /michigan/jobs/
3. RedirectMatch 302 ^/michigan/jobs/?/jobs/ /michigan/jobs/
4. RewriteRule ^/michigan/jobs/?/jobs/$ /michigan/jobs/ [R=301,L]
RewriteRule ^michigan/jobs/?/jobs /michigan/jobs/
I've tried a bunch of different "generators" but they don't seem to detect the random "?" in the middle of things.
What would be the correct redirect or rewrite rule to handle this?
not like a typical query string
Although that IS a query string, just like any other. Anything after the first ? is a query string, whether that is the intention or not is another matter. So, in your example /jobs/ is the query string.
The Redirect, RedirectMatch and RewriteRule directives all match against the URL-path only, which notably excludes the query string.
To match the query string, you need to use mod_rewrite and an additional RewriteCond directive (a "condition") that compares against the QUERY_STRING server variable, together with a RewriteRule.
For example:
RewriteEngine On
# Removes the query string from "/michigan/jobs/?/jobs/"
RewriteCond %{QUERY_STRING} ^/jobs/$
RewriteRule ^michigan/jobs/$ /$0 [QSD,R=301,L]
The $0 backreference contains the full match from the RewriteRule pattern - basically the full URL-path. This just saves repetition.
The QSD (Query String Discard) flag then discards the original query string from the redirect response, otherwise it is passed through by default.
Note that the URL-path matched by the RewriteRule pattern does not start with a slash.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
I have:
https://Restoran.Grupacija.com/?ćumur
Which needs to be redirected to:
https://Restoran.Grupacija.com/
This directive is in .htaccess file in the Root Folder, but it does not work:
Redirect 301 "^/%3Fćumur" https://Restoran.Grupacija.com
I have also tried this, but it does not work either:
Redirect 301 "^/%3F%C4%87umur" https://Restoran.Grupacija.com
My .htaccess file is UTF-8 already.
The mod_alias Redirect directive matches the URL-path only, which notably excludes the query string. (However, neither does the Redirect directive match using a regex, it uses simple prefix-matching instead.)
To match the query string you need to use mod_rewrite instead and match against the QUERY_STRING server variable in a RewriteCond directive.
Note that the browser/user-agent will URL encode the ć as %C4%87 in the HTTP request and the QUERY_STRING server variable is not %-decoded, so we need to match the encoded request. Strictly speaking, both %C4 and %c4 are valid encodings.
I'm assuming you don't need to explicitly check the requested hostname, unless this query string could be valid on another hostname that your server receives?
For example, to perform the redirect as requested, the following would need to go near the top of your .htaccess file before any existing rewrites.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^%[cC]4%87umur$
RewriteRule ^$ / [QSD,R=301,L]
The QSD (Query String Discard) flag removes the query string from the redirect response.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
(You do not need to repeat the RewriteEngine directive if this already occurs in your .htaccess file.)
What the best .htaccess rewrite code to force URL like this one
https://www.example.com/index.php?page=report/tinytoes_mothercare
to that?
https://www.example.com/report/tinytoes_mothercare
My web site already resolve https://www.example.com/report/tinytoes_mothercare, so I want to avoid any duplicate contents with the ugly and bad URL above.
You can do something like the following at the top of your .htaccess file, before the existing rewrite (that "resolves" the pretty URL) to redirect the old URL with a query string to your new pretty URL.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^page=([\w/-]*)
RewriteRule ^index\.php$ /%1 [QSD,R=302,L]
The first condition that checks against the REDIRECT_STATUS environment variable ensures we don't get a redirect loop by redirecting the rewritten URL. REDIRECT_STATUS is empty on the initial request, so only redirects direct requests from the client.
The second condition gets the necessary value from the query string page URL parameter. This matches characters a-z, A-Z, 0-9, _ (underscore), / (slash) and - (hyphen) - as in your example URL. If you need to match more variation then the character class will need to be expanded. This value is then used in the RewriteRule substitution string using the %1 backreference.
Note that this is a 302 (temporary) redirect. Only change to a 301 (permanent) when you are sure it works OK, in order to avoid caching issues.
In phpmydirectory framework, I'm trying to redirect old page URL to new page URL using .htaccess 301 redirect.
ex: http://example.com/pages/newurl.html?id=old-friendly-url
Redirect works but at the end of new URL, old friendly URL is concatenated as id.
How can I avoid that?
RewriteRule ^pages\/(.+)\.html$ page.php?id=$1 [L,NC]
Redirect 301 /pages/features.html /pages/ost-to-pst-ost-to-office365-ost-to-exchange-migrator.html?
Redirect 301 /pages/features.html /pages/ost-to-pst-ost-to-office365-ost-to-exchange-migrator.html?
Presumably this is the redirect in question? If it already contains a ? at the end then the query string on the original request should already be getting removed - however, if using a mod_alias Redirect you will end up with a trailing ? on the target URL.
You should use mod_rewrite instead to perform this redirect. Since you are already using mod_rewrite for internal rewrites, this is recommended in order to avoid potential conflicts.
Your original redirect is not checking the query string, so I presume this is not a required - you only need to match the URL-path.
The following redirect should go before your existing internal rewrite:
RewriteRule ^pages/oldurl\.html$ /pages/newurl.html? [R,L]
The trailing ? on the RewriteRule substitution removes the query string from the request. Alternatively, use the QSD flag on Apache 2.4+
We have some proplem with seo and need rewrite pages with id
from
example.com/media/player/related.php?mode=related&video_id=12345
to
example.com/video/12345
All id parameters have numerical values. RewriteEngine On
How can we achieve this?
In order to redirect from /media/player/related.php?mode=related&video_id=12345 (containing a query string) to /video/12345 then you could do something like the following near the top of your .htaccess file.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^mode=related&video_id=(\d+)
RewriteRule ^media/player/related.php$ /video/%1 [R=302,L]
This matches the source URL as mentioned in the example, except the value of the video_id URL param is captured and assumed to be numeric only. This also just tests that the query string starts with the stated query string, so any additional URL params are ignored.
Change the 302 (temporary) status to 301 if this is intended to be a permanent redirect, but only after you have checked that it's working OK. (301s are cached hard by the browser so can make testing problematic.)
Presumably, you have existing directives or some kind of front controller that already rewrites /video/12345 back to the "real" URL? The check against the REDIRECT_STATUS environment variable is to prevent a redirect loop, assuming this is the case.