I'm stuck with an 301 redirect in htaccess. I'd like to redirect from
https://subdomain.example.com/SOME_STRING
to
https://www.example.com/folder/SOME_STRING
The string can be different on any request. Can someone help me out here?
If these two hostnames point to the same place then you'll need to use mod_rewrite in order to check the hostname being requested.
Try the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.(example\.com) [NC]
RewriteRule ^ https://www.%1/folder%{REQUEST_URI} [R=301,L]
The %1 backreference matches the example.com in the preceding CondPattern - this simply saves repetition. The REQUEST_URI server variable contains the full URL-path from the request, including the slash prefix.
In fact, this is almost identical to another recent question that matches any subdomain:
how to redirect all subdomains to another with parameter after the trailing slash using htaccess?
To hardcode the hostname in the substitution string (as mentioned in comments) then you would do the following:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com [NC]
RewriteRule ^ https://www.example.com/folder%{REQUEST_URI} [R=301,L]
Related
I have posts like so:
http://example.com/blog/post-title
I want to convert this to
http://example.com/post-title
Using htaccess
I tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com/blog/([^\s\?]+)\s [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
In this situation, you can indicate two capture groups (using parentheses) and only return the second of the two groups.
Example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(blog)/(.+) http://example.com/$2
</IfModule>
The rewrite rule works as follows:
If...
the url starts with blog
followed by /
followed by at least one or more characters
then rewrite the URL so that the domain is followed by a / and then the one or more characters that were captured in the second capture group.
N.B. You won't need an [R=301] flag since providing a full redirect (ie. a redirect starting with the http:// or https:// protocol) implicitly indicates that this will be a 301 Redirect.
If I use anything in wildcard it's redirecting me to example.com/shop. But what I want is when someone uses anything.example.com/code he redirects to example.com/shop/code. The current setting is redirecting me to example.com/code/shop if I use anything after the trailing slash in my case it's code.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI}/shop [R=301,L,NE]
You would seem to just need to reverse the %{REQUEST_URI}/shop part in the substitution string... (?)
For example:
RewriteCond %{HTTP_HOST} ^[^.]+\.(example\.com)$ [NC]
RewriteRule ^ http://%1/shop%{REQUEST_URI} [R=301,L,NE]
The REQUEST_URI server variable contains the full URL-path (only) that the user requested, including the slash prefix.
You will need to clear your browser cache before testing. (Preferably test first with 302 temporary redirects to avoid caching issues.)
i'm going to redirect from
www.example.com/forum or example.com/forum
to
www.example.ir/forum
using htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.ir/forum$ [OR]
RewriteCond %{HTTP_HOST} ^example.com/forum$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com/forum$
RewriteRule (.*) http://www.example.ir/forum$1 [R=301,L]
i fail to do that and i always got:
www.example.irforum (without slash /) which is dead link.
The code you have posted could not possibly redirect to www.example.irforum (without slash). In fact, the code you have posted won't do anything (since none of the conditions will match). If you are seeing a redirect then it's possibly a previously cached (erroneous) redirect.
Assuming example.com and example.ir are pointing to the same place then try the following in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^forum(.*) http://www.example.ir/forum$1 [R=302,L]
The above rules are processed as follows:
Does the URL-path (less the directory prefix) of the request match the RewriteRule pattern ^forum(.*)? If so, then continue...
Does the HTTP_HOST server variable match the CondPattern. ie. Is the Host header either www.example.com or example.com? If so, then continue...
Redirect the request to http://www.example.ir/forum$1 - this is the RewriteRule substitution. $1 refers to the captured group (first parenthesised subpattern) in the RewriteRule pattern - ie. whatever matches (.*).
If example.com and example.ir point to different servers then you don't need the RewriteCond directive.
Test with 302 (temporary) redirects until you are sure it's working OK then change to 301 (permanent). Or, test with browser caching disabled.
The HTTP_HOST variable contains just the HTTP Host header. eg. example.com. This contains no path information. Use the RewriteRule pattern to match the URL-path.
My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.
Have looked at all the other posts regarding this, but can't seem to get it to work. There are a number of unrelated reasons why I can't change the Joomla config, items, etc., but in any event, it seems that these should work regardless of that.
In short, I want any link with Itemid=30 in it to redirect to the one given. What am I doing wrong? The first 3 are what I've tried, that last line is one that is working.
RedirectMatch 301 ^Itemid=30$ http://inside.beta.oursite.com/index.php?option=com_cware&view=courses&Itemid=125
redirect 301 index.php?option=com_cware&view=courses&Itemid=30 http://inside.beta.oursite.com/index.php?option=com_cware&view=courses&Itemid=125
RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=30
RewriteRule ^Itemid=30/$ index.php?option=com_content&view=article&id=106&Itemid=121 [R=301]
# If query string contains "username=admin"
RewriteCond %{QUERY_STRING} username=admin
# Block it without mercy
RewriteRule .* - [F]
Redirect considers only URL paths, which excludes the query string. If you want to take the query string into account, you must employ mod_rewrite.
Same applies to RewriteRule
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Therefore, if you want to match some query string and redirect all URLs to some other URL, use a RewriteCond in combination with RewriteRule
RewriteEngine On
RewriteCond %{QUERY_STRING} Itemid=30
RewriteRule .* index.php?option=com_content&view=article&id=106&Itemid=121 [R=301]
This redirects any URL .* with the query string containing Itemid=30 to index.php?option=com_content&...