Prevent redireting for a specific URL - .htaccess

I'm redirecting if the url contains a specific word.
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
Is it possible to avoid redirecting for a specific url that has the above specific word?
E.g.
The below gets redirect
http://www.exsample.com/forum/contactus.html?ypin9001234
to
http://www.exsample.com/homepage.html?no=ypin9001234
What I need is to continue the above redirect as it is but to avoid the redirect only for the below URL.
http://www.exsample.com/forum/members/gold/gold.html?ypin9001234
Note: The word gold is used only by this url.

You should use a RewriteCond with THE_REQUEST to match full URL with query string:
RewriteCond %{THE_REQUEST} !/forum/members/gold/gold\.html\?ypin9001234 [NC]
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]

You can add a rewrite condition before your rewrite rule, e.g.:
RewriteCond %{REQUEST_URI} !^.*gold.*$
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
This will prevent any url containing the word gold from being rewritten. If you need to be more specific, just make the condition more precise:
RewriteCond %{REQUEST_URI} !^forum/members/gold.*$
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
etcetera...

Related

Rewrite url to url without html ending (.html)

I would like to redirect (301) all urls without .html to .html.
Example:
example.com/site should be redirected to example.com/site.html
I tried the following:
RewriteRule (.+)$ /$1.html [L,R=301]
Your rule is likely to cause a redirect loop, because it would rewrite the URL even after it has already been redirected.
In your pattern you need to make sure that the URL does not end with .html already.
You could use a negated pattern to achieve this:
RewriteRule !\.html$ %{REQUEST_URI}.html [L,R=301]
Alternatively, if you do not want to apply this for empty paths (such as in your example) or need to reuse the matches of your pattern, you could use a RewriteCondition:
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule (.+)$ $1.html [L,R=301]
Note that this is a very simple example which only works for the exact case you described. This would also rewrite URLs ending with .htm to .htm.html, which might or might not be what you want.

How to redirect all URLs that do not have a parameter to another URL

I wonder how to redirect all URLs in .htaccess that do not have a parameter to another URL.
Example:
Any access without "&tag=xxx" redirects to index.php?tag=xxx
You can use this rule at top of your site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)tag=xxx(?:&|$) [NC]
RewriteRule ^(index\.php)?$ %{REQUEST_URI}?tag=xxx [L,NC,R=302,QSA]
Negative RewriteCond %{QUERY_STRING} checks whether query parameter is NOT present and RewriteRule adds the missing query parameter and does a full redirect.

Redirect to different location based on presence/absence of a parameter

Is there any way to differentiate a Redirect on the basis of whether it has parameters or doesn't?
Example:
I want to redirect everything targetted at
http://www.example.com/news.php?id=123
to
http://www.example.com/old-news.php?id=123
but if no parameter is given, it should redirect to
news-overview.php, same path.
Cheers!
You'll need to use mod_rewrite and the %{QUERY_STRING} variable in order to do this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=
RewriteRule ^news\.php$ /old-news\.php [L,R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^news\.php$ /news-overview\.php [L,R]
note that the query string automatically gets appended to the redirected URL. You can change the R to R=301 if you want to permanent redirect.

.htaccess redirect to subdomain based on query string parameter

I need to set up a redirection to sub domain based on query string.
Example:
http://example.com/message.php?id=subdomain to http://subdomain.example.com/message.php
Is this possible in .htaccess?
This will redirect all the URLs with QUERY_STRING like id=subdomain and file message.php to the appropriate subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule message.php http://%1.example.com/message.php? [R=301,L]
Mind the question mark at the end of message.php? - it is used to suppress appending the old QUERY_STRING. I added the domain name check to avoid the possibility of eternal redirect loop.

htaccess redirects are "ignored"

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&...

Resources