I want to have RewriteRule where domain's /page/test or /page/test/ go to www.domain.com. Only for this two example.
I try:
RewriteRule ^/page/test/?$ http://www.domain.com [R=301,L,NC]
This will be good but I have other pages like /page/test/?D_test
and when I put RewriteRule, redirect also /page/test/?D_test. Problem is '?'. How should I write my rule to don't rewrite another page's to www.domain.com ? for example
/page/test/?D_test /page/test/(.*)
I'm not sure I understand your question. You are either asking how can I redirect all the traffic including the query string to the domain and take off the query string. In that case I usually do it this way:
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]
If you want to make it so it doesn't redirect if there is a query string then you have to use a rewrite conditional.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]
Related
i have n URL like this:
http://name.co/othername/xname
http://name.co/othername/yname
So name.co/othername are equal. Just the 'xname' is changing to a different.
I need a redirect to the HTTPS Side.
I tried this:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
but the result was not correct. it would be nice if someone could help me
REQUEST_URI will exclude the query string. Also, you need flags, at least [R] and usually [L] as well. You will probably want a 301 redirect in this case, unless you think you are likely to turn this off later. Try replacing the RewriteRule you have with:
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
The R flag means to actually send a redirect to the browser as opposed to just changing how the URL is handled insternally; the L flag means this is the last directive that should be processed; it shortcuts the rest of the processing and avoids any additional changes made by other rules.
Okay, so I have two separate mod-rewrite rules in my vhost block. The first rule redirects a customer offsite if they come in thru an affiliate URL such as example.com/1234.html and the second rule forces the URL to always contain www dot like www.example.com.
# Affiliate Links
RewriteRule ^([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
# Ensure we are always on www dot
RewriteCond %{HTTP_HOST} ^example\.loc [NC]
RewriteRule (.*) http://www.example.com$1 [R=301,L]
The rules themselves work great. The problem is that if the first rule applies I want it to immediately redirect, however it seems as if the second rule is hoisted to the top because it always takes precedence. What do I need to change so that these execute in order?
You've stated that this is in a vhost block. In that context (as opposed to, for example, an .htaccess file) URLs always start with '/'
Thus
RewriteRule ^([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
should instead be
RewriteRule ^/([0-9]+)\.html$ http://affiliates.example.com/log.php?id=$1 [R=302,L]
(ie, with the leading slash), otherwise it will never match anything.
I am migrating data from one content management system to another. There is no relationship between old URLs and new URLs, although both contain query strings. I am trying to set up a set a rewrites that will redirect broad category lists of data from one to the other.
Here's a sample:
OLD
rss.php?categoryID=53
NEW
index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19
I tried
RewriteRule ^rss.php\?categoryID=53 index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
but it doesn't match. If I follow that one with
RewriteRule ^rss.php index.php?module=news&type=user&func=list&tid=1 [L]
if DOES match, so I conclude that the question mark in the old URL is causing the problem. I am already escaping the question mark. What do I do?
I will probably end up with about 50 of these in my .htaccess file.
You can't match against the query string (all that stuff after the ?) in a RewriteRule, you need to use a RewriteCond and match against the `%{QUERY_STRING} var:
RewriteCond %{QUERY_STRING} ^categoryID=53$
RewriteRule ^rss\.php$ /index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
Or change the brackets to [R=301,L] if you want to redirect the browser.
Hi. I am new to mod_rewrite and was wondering if it was possible to do the following:
RewriteRule ^([^/]*)$ index.php?slug=$1
This rule will point only at index.php but if I wanted to do another rule that pointed a specific slug to a different script, i.e.
RewriteRule ^a-new-page$ different.php
This would not work because the first rule has declared anything that is entered should point at index.
Is there a way to force the new rule for that specific slug?
Yes, it is possible -- just place such specific rule before generic/broad one (the order in which rules are declared matters):
RewriteRule ^a-new-page$ different.php
RewriteRule ^([^/]*)$ index.php?slug=$1
Also please pay attention to the broad rules -- they may enter into a rewrite loop (after rewrite new URL goes into next iteration, and if rule is not written right it may enter endless loop which Apache will have to forcedly terminate and your user will see an 500 error page instead). Better rule will be:
# specific rule
RewriteRule ^a-new-page$ different.php [L,QSA]
# broad rule (will be triggered if requested resource is not a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?slug=$1 [L,QSA]
[L] tells Apache to stop rewriting if this rule matches
[QSA] tells to append query string to a new URL (useful if you have URL parameters: e.g. tracking data, page parameters etc).
Useful Link: http://httpd.apache.org/docs/current/rewrite/
I am researching a way to work filtering specific pages by IP and redirect them on a different page.
The code below, did not work properly.
RewriteCond %{REMOTE_ADDR} ^/192.168.10.*
RewriteCond %{REQUEST_URI} ^/support
RewriteRule ^/.* http://www.yahoo.com/gone [R,NE]
Once the link http://example.com/support has been accessed and they're on the 192.168.10.* block, it must go to the yahoo.com example page.
But, like I said. It just did nothing. Any ideas why it did not work correctly?
as yoda says in the comment, don't put a / in front of the ip address. also, the . in the pattern should be \., as this is a perl compatible regular expression. you could also add a [NC], no case (sensitive), to the request uri match. finally, you could merge the second condition with the RewriteRule. all together:
RewriteCond %{REMOTE_ADDR} ^192\.168\.10\..*
RewriteRule ^/support http://www.yahoo.com/gone [R,NE,NC]