What I'd like to happen via a .htaccess redirect is:
An url request of : "http://subdomain1.mysite.com/addToBasket.php?query_string"
is redirected to
"http://subdomainA.subdomain1.mysite.com/addToBasket.php?query_string"
The subdomains 'subdomain1' and 'subdomainA.subdomain1' are properly set up in DNS as fully qualified domain names
However, subdomain1.mysite.com exists on a different server than subdomainA.subdomain1.mysite.com - so absolute url format is required throughout.
In the request site subdomain directory the 'addToBasket.php' script file will not exist but will exist in the redirected to subdomain directory
The requested query string is a wildcard but must not be altered during the redirection.
I've searched and tried many of the example solutions - but the '?query string' part is always being omitted in the redirection.
Reason for the subdomain1 => subdomainA.subdomain1 madness and differing servers is that a new site is being launched on the new server with a fresh start DB but the old site generated basket related emails to customers containing links which they clicked to fulfil the order process - so the old site will still be hosted on the old server for a while but under a subdomain just to allow time for these email links still in the wild to work with the system they were designed for.
Unfortunately the new developers created a similarly named subdomain system as used in the old site - so the old site will now have a 'subdomain.subdomain' url format to allow it to have a different IP and function.
Any help very much appreciated!
Unless you explicitly overwrite the query string, it will be preserved automatically, see RewriteRule
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
This means, if you just redirect without adding a query string yourself, it will be passed on, e.g.
RewriteRule ^addToBasket.php$ http://subdomainA.subdomain1.mysite.com/addToBasket.php [L]
or if you want to redirect not just this one, but every possible request
RewriteRule ^ http://subdomainA.subdomain1.mysite.com%{REQUEST_URI} [L]
Related
After site crash a redirect php script doesn't work as expected.
We try to fix it, but in the meantime we are looking for a quick solution to redirect search engine results so our visitors can at least visit after clicking a relative web page.
The url structure or the search engines result are something like this:
https://www.example.com/MainCategory/SubCategory_1/SubCategory_2/Product?page=1
and I'd like to redirect using the "SubCategory_2" part of the URL to something like this
https://www.example.com/SubCategory_2.php
so until we fully repair the script at least our visitors will se a relative web page.
I'm quite stuck... Any ideas?
Thank you
To redirect the stated URL, where all parts are variable (including an entirely variable, but present query string) then you can do something like the following using mod_rewrite near the top of your root .htaccess file (or crucially, before any existing internal rewrites):
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^[^/]+/[^/]+/([^/]+)/[^/]+$ /$1.php [QSD,R=302,L]
The QSD flag is necessary to discard the original query string from the redirected response.
The above will redirect:
/MainCategory/SubCategory_1/SubCategory_2/Product?page=1 to /SubCategory_2.php
/foo/bar/baz/qux?something to /baz.php
You can test it here using this htaccess tester.
UPDATE:
unfortunately without success. I get 404 error.
You'll get a 404 if the directive did not match the requested URL, or /SubCategory_2.php does not exist.
Is the URL redirected? What do you see in the browser's address bar?
If there was no redirect then the above rule did not match the requested URL and the rule did nothing. Either because:
The URL format is not as stated in the question.
The rule is in the wrong place in the .htaccess file. As stated, this rule needs to be near the top of the config file.
I found a basic solution here htaccess redirect if URL contains a certain string I crate something like this RewriteRule ^(.*)SubCategory_2(.*)$ https://example.com/SubCategory_2.php[L,R=301] and works just fine. My problem is that this is a "static solution" since "SubCategory_2" is a variable.
Ok, but that is a very generic (arguably "too generic") solution for the problem you appear to be attempting to solve. This matches "SubCategory_2" anywhere in the URL-path (not just whole path segments) and preserves any query string (present on your example URL) through the redirect. So, this would not perform the stated redirect on the example URL in your question.
However, the directive you've posted (which you say "works just fine") cannot possibly work as written, at least not by itself. Ignoring the missing space (a typo I assume) before the flags argument, this would result in an endless redirect loop, since the target URL /SubCategory_2.php also matches the regex ^(.*)SubCategory_2(.*)$.
Also, should this be a 301 (permanent) redirect? You seem to imply this is a "temporary" solution?
HOWEVER, it's not technically possible to make "SubCategory_2" entirely variable in this "basic solution" and search for this variable "something" in a larger string and redirect to "something.php". How do you know that you have found the correct part of a much larger URL? You need to be more specific about what you are searching for.
In your original question you are extracting the 3rd path segment in a URL-path that consists of 4 path segments and a query string. That is a perfectly reasonable pattern, but you can't extract "something" when you don't know what or where "something" is.
We made URL permalink changes to WordPress sites, removing a no longer needed date in URLs and keeping just the domain, subfolder and postname.
from /%year%/%monthnum%/%day%/%postname%/
to /%postname%/
While the 301 redirect is working perfectly we have hundreds of URL references in post contents that we'd like to update at once with WP-CLI. Does anyone know how to create a regular expression SQL command for this in WP-CLI to remove the date from URLs, making all internal links current again?
I run an Expression Engine site that will load an article page regardless of any made up sub directory names typed into the address bar.
For example all these addresses:
http://hellothere.example.com/health/mental/depression
http://cats.example.com/health/mental/depression
http://dogs.example.com/health/mental/depression
http://www.batman.example.com/health/mental/depression
http://www.1.2.3.4.5.6.example.com/health/mental/depression
Will load example.com/health/mental/depression
Obviously, this is less than ideal for SEO since I've got a potentially unlimited number of duplicate URLs.
I am trying to figure out how to use htaccess to strip anything before example.com and replace it with just www.
Any help would be appreciated!
You can not do this with htaccess. The way to do this is to set up a wildcard dns record so that all requests (*.example.com) are all directed to the same server.
See http://codex.wordpress.org/Configuring_Wildcard_Subdomains
I have a php site and the URLs are displayed as follow:
http://www.example.com/cheap-call-single.php?country=ALBANIA
I want to re-write it, to display as:
http://www.example.com/cheap-international-calls-ALBANIA.php
I have searched many only generators and they all say to use:
RewriteEngine On
RewriteRule ^cheap-international-calls-([^/]*)\.php$ /cheap-call-single.php?country=$1 [L]
But this is not working for me, please note that the server has the correct Apache setting enabled.
Can someone please help me with the correct syntax?
The rewriteRule you show does the inverse, it takes incoming url in the long form and translate it to the query string version (with ?).
Now the problem is what do you mean when you say: "I want to re-write it, to display as:"
The display and the rewrite are usually different things:
If the 'display' is the url seen by the user in his browser you have :
to push this way of writing urls in your application, so that the received HTML contains the right display, this has nothing to do with mod_rewrite
you may optionally perform HTTP redirections with mod_rewrite, so when you detect the old syntax (cheap-call-single.php?country=ALBANIA) you redirect the user on the right one, then the request is re-executed by the browser (and then you should have a cheap-international-calls-ALBANIA.php file on your server, else it's a final 404)
If you do not have this file on the server (so, what you have is cheap-call-single.php) then the exposed rewriteRule is right and back to step one, it's your application which should show the right url on the HTML side.
Now if your really have cheap-international-calls-ALBANIA.php and you want your application to rewrite incoming request using cheap-call-single.php to this file, based on query string parameters, you'll have some problems. Writing rules on the query string part of the request is always complex, query string arguments may appear in any order, may be written with urlencoding or not, etc. By default rewriteRules are not using the query string part.
This is something like:
RewriteCond %{REQUEST_FILENAME} cheap-call-single\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|%26|%20)country(=|%3D)([^&]+) [NC]
RewriteRule .* /cheap-international-calls-%3.php [L,R]
Untested (not sure for the \.), and yet not managing the fact each letter in the country word would be urlencoded, and not managing the upcase of the country name. You would need a RewriteMap to transform it uppercase. But already have my headache, is this what you really need?
One of my clients (before I came along) decided to use htaccess redirects as their form of URL shortening/search engine friendly URLs. They have literally thousands of them.
The new version of the site now has friendly urls but they aren't equivalent to their redirects so they still need them.
My question to you all is: Is there another way than to populate this file with thousands of lines of "Redirects /folder1 /folder2"?
Thanks
If you cannot make simple rules to catch all of them as in the #chris henry solution you can use the RewriteMap utility of mod_rewrite. You'll be able to write these thousand rules in a text file, then make this text file an hash file, and mode_rewrite will try to match url in this file (if it's an hash file it's quite fast). After that mode_rewwrite can generate a redirect 301 with the [L,R=301] tag.
Yep, look at using the Apache config (httpd.conf or httpd-vhosts.conf) to set up site wide folder aliasing. Eg:
Alias /folder1 c:/www/folder2
Look at http://httpd.apache.org/docs/2.0/mod/core.html#directory for more info.
Depending on how different the URLs being redirected are, one solution might be to come up with an rewrite rule that covers all of them, and maintain the short / long URLs in your application, or even a database.