Trying to redirect index.php?page=11. (with the period) to index.php?page=11 (no period). This is a page that somehow was indexed in Google, that breaks with the period in the URL.
RewriteRule ^index\.php\?page\=11\.$ index.php?page=11 [R=301]
This is not catching.
You can't match against the query string (everything after the ?) in a rewrite rule, you need to match against the %{QUERY_STRING} variable in a condition:
RewriteCond %{QUERY_STRING} ^page=11\.$
RewriteRule ^index\.php$ /index.php?page=11 [L,R=301]
Related
I have been at this all day long, tried dozens of variations but can't quite seem to get this rewrite to work.
RewriteCond %{THE_REQUEST} /pwreset\.php\ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ https\:\/\/www\.example\.com\/support\/pwreset\.php [L]
The URL it returns is:
https://www.web-jive.com/support/pwreset.php/?key=cdc3b1aa842785f7345be501a30ddc83
What I need to be removed is the pwrest.php trailing slash before the question mark. Where am I going wrong on this?
The idea is to have the first URL below, redirect to the second:
https://example1.com/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
https://example2.com/support/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
EDIT
Per Mr. White's suggestion, I'm posting the whole .htaccess file.
RewriteEngine On
# Announcements
RewriteRule ^announcements/([0-9]+)/[a-z0-9_-]+\.html$ ./announcements.php?id=$1 [L,NC]
RewriteRule ^announcements$ ./announcements.php [L,NC]
# Downloads
RewriteRule ^downloads/([0-9]+)/([^/]*)$ ./downloads.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^downloads$ ./downloads.php [L,NC]
# Knowledgebase
RewriteRule ^knowledgebase/([0-9]+)/[a-z0-9_-]+\.html$ ./knowledgebase.php?action=displayarticle&id=$1 [L,NC]
RewriteRule ^knowledgebase/([0-9]+)/([^/]*)$ ./knowledgebase.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^knowledgebase$ ./knowledgebase.php [L,NC]
#Password reset
RewriteCond %{QUERY_STRING} ^key=[0-9a-f]{32}$
RewriteRule ^pwreset\.php$ https://www.web-jive.com/support%{REQUEST_URI} [R=302,L]
#Redirect to new support URL
RewriteCond %{HTTP_HOST} ^members\.web\-jive\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.members\.web\-jive\.com$
RewriteRule ^/?$ "https\:\/\/www\.web\-jive\.com\/support" [R=301,L]
RewriteCond %{HTTP_HOST} ^members\.web\-jive\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.members\.web\-jive\.com$
The output you are seeing (with the trailing slash on the URL-path) isn't the result of just the directives you posted, so maybe you have a conflict with other directives or you are seeing a cached response.
However, the rule you posted would seem to be far more complex than it needs to be.
1. https://example1.com/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
2. https://example2.com/support/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
Assumptions:
You don't need to match the key value; just the URL-path (ie. /pwreset.php)
example1.com and example2.com point to different places (the filesystem does not overlap).
To redirect from 1. to 2. try the following at the top of your .htaccess file in the root of example1.com:
RewriteEngine On
RewriteRule ^pwreset\.php$ https://example2.com/support%{REQUEST_URI} [R=302,L]
Any query string (eg. key=abc...) is passed through unaltered.
Note that this is a 302 (temporary) redirect. Only change it to a 301 (permanent) when you have confirmed it works OK.
If you need to check that a key= URL param is present and is set to a 32 hex string (which appears to be what your example represents) then include a condition before the above RewriteRule that checks against the QUERY_STRING server variable. For example:
RewriteCond %{QUERY_STRING} ^key=[0-9a-f]{32}$
RewriteRule ^pwreset\.php$ https://example2.com/support%{REQUEST_URI} [R=302,L]
If any other URL params are present on the request then the redirect will fail.
Aside:
RewriteRule ^(.+?)/$ https\:\/\/www\.example\.com\/support\/pwreset\.php [L]
This looks very cPanel-esque. There is no need to backslash colons, slashes and dots in the RewriteRule susbtitution argument. This is an "ordinary" string, not a regex. These characters have no special meaning here.
I am trying to create a htaccess file for my site.
I would like it so that when a user enters http://sitename.co.uk/category/productname/?pid=productID they are shown the page http://sitename.co.uk/product.php?id=productID
So the category and the productname are essentially wildcards and can be anything as the page is powered by the productID.
I have tried writing the following but have had no luck with it...
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/(?pid=.*)$ product.php?id=$1 [L,NC]
You should can try like this
RewriteCond %{QUERY_STRING} pid=([^&]+)
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+) /product.php?id=%1
%1 is back-reference for RewriteCond. $1 is back-reference for RewriteRule.
You will need to capture value of pid parameter from a RewriteCond as RewriteRule doesn't match query:
RewriteCond %{QUERY_STRING} ^pid=([^&]+) [NC]
RewriteRule ^([\w-]+)/([\w-]+)/?$ product.php?id=51 [L,NC,QSA]
I have dozens of redirects from an old page e.g. index.php?mode=1,2,3,0 and I want to get rid of all GET Params because the new page is anyways just plain html.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} mode=17,0,0,0,0$
RewriteRule (.*) /big-mamas-house/ [R=301,L]
I thought removing (.*) would already do the trick but then the rule is not applied anymore according to:
http://htaccess.madewithlove.be/
Your rule can simplified to:
RewriteCond %{QUERY_STRING} ^mode=17,0,0,0,0$
RewriteRule ^index\.php$ /big-mamas-house/? [R=301,L,NC]
? in the end is needed to strip off any previous query string.
I have a few links that look like this, that unfortunately are being used:
http://www.example.com/page/?
http://www.example.com/another-page/??
Is it possible to have them redirect without the question mark(s), or strip them out?
I've tried:
RewriteRule /page/? http://example.com/page/ [R=301,L]
RewriteRule /page/\? http://example.com/page/ [R=301,L]
RewriteRule ^/page/\? http://example.com/page/ [R=301,L]
RewriteRule ^/page/\?$ http://example.com/page/ [R=301,L]
Redirect 301 /page/? http://example.com/page/
You can't match against the query string (or even the ?) in a rewrite rule. Since the query string itself is empty, you can't even match against the variable, %{QUERY_STRING}. What you need to do is match against the actual request:
RewriteCond %{THE_REQUEST} \ /+([^\ \?]+)\?(\ |$)
RewriteRule ^(.*)$ /$1? [L,R=301]
It's going to look real weird with a ? at the end of the rule's target, but that's how you tell mod_rewrite to remove the query string.
My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here's what I thought should remove % on the query string and replace with A but it's not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can't quite spot it. Thanks for the expert eyes!
You're real close.
The problem here is that you've got a condition and the match of your rule should be together. Your backreference to the previous RewriteCond is broken because it's for the REQUEST_URI and not the QUERY_STRING like you want.
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
Here, the %1 backreference matches the (.*) at the end of the /SpecialPage URI. The backreferences from your query string match gets lost, and that's the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteRule ^SpecialPage(.*)$ /SpecialPage$1?%1A%2 [L]
Here, the %1 and %2 backreferences correctly reference the query string and the SpecialPage condition in the URI is met by the regex pattern.
Redirect Query String
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)=(.)$
RewriteRule ^(.)?(.)$ /$1--%0? [R=301,L]
From Url: http://localhost/sholay-slide.jsp?slide=2
To Url: http://localhost/sholay-slide.jsp--slide=2