I have the following rewrite rule in place:
RewriteRule ^register/?$ /fitch/index.php?cmd=register [NC,L,QSA]
Requests to mysite.com/register are rewritten to
mysite.com/fitch/index.php?cmd=register
I want to allow an optional parameter to be passed as well, such that requests to
mysite.com/register/sender=models directs me to
mysite.com/fitch/index.php?cmd=register&sender=models
Perhaps:
RewriteRule ^register/(.*) /fitch/index.php?cmd=register&$1
For any number of paths -> query string params:
RewriteCond %{REQUEST_URI} ^/register/
RewriteRule ^(.*)/(.*)$ /$1&$2 [L]
RewriteCond %{REQUEST_URI} ^/register&
RewriteRule ^(.*)$ /fitch/index.php?cmd=$1 [L,QSA]
This will rewrite URL's like:
http://mysite.com/register/a=b/1=2/c=d
to:
http://mysite.com/fitch/index.php?cmd=register&a=b&1=2&c=d
Related
I'm trying to accomplish some redirects as follows:
#Basic Rule Set
RewriteRule ^/path$ http://newhost.com/Page?pagename=form1000 [R=301]
RewriteRule ^/path/index.html$ http://newhost.com/Page?pagename=form1000 [R=301]
which work fine.
However, redirects with query strings in source and destination return me to the above destination url. For e.g.,
# Advanced Rule Set
RewriteRule ^/path/index.html?var=foo$ http://newhost.com/Page?pagename=form1000?id=hello [R=301]
RewriteRule ^/path/index.html?var=bar$ http://newhost.com/Page?pagename=form1000?id=byebye [R=301]
RewriteRule ^/path/index.html?var=xyz$ http://newhost.com/Page?pagename=form1000?id=world [R=301]
all redirect to http://newhost.com/Page?pagename=form1000.
I've tried a RewriteCond %{QUERY_STRING} ^var=(.*)$ [NC] and RewriteCond %{REQUEST_URI} ^/path/index.html [NC] preceding the above three rules, and still I'm redirected to http://newhost.com/Page?pagename=form1000. I've swaped the order of the Basic and Advanced rule sets, and all redirect to http://newhost.com/Page?pagename=form1000.
Any ideas on how I might get these rule sets to work? CentOS 6.x, Apache 2.2.
You may try this instead:
# Advanced Rule Set
RewriteCond %{QUERY_STRING} var=foo [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=hello [R=301,L,NC]
RewriteCond %{QUERY_STRING} var=bar [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=byebye [R=301,L,NC]
RewriteCond %{QUERY_STRING} var=xyz [NC]
RewriteRule ^path/index.html/? http://newhost.com/Page?pagename=form1000?id=world [R=301,L,NC]
I have a lot of working mod_rewrite rules already in my httpd.conf file. I just recently found that Google had indexed one of my non-rewritten URLs with a query string in it:
http://example.com/?state=arizona
I would like to use mod_rewrite to do a 301 redirect to this URL:
http://example.com/arizona
The issue is that later on in my rewrite rules, that 2nd URL is being rewritten to pass query variables on to WordPress. It ends up getting rewritten to:
http://example.com/index.php?state=arizona
Which is the proper functionality. Everything I have tried so far has either not worked at all or put me in an endless rewrite loop. This is what I have right now, which is getting stuck in a loop:
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteRule .* http://example.com/arizona [R=301,L]
#older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
which gives me an endless rewrite loop and takes me to this URL:
http://example.com/arizona?state=arizona
I then tried this:
RewriteRule .* http://example.com/arizona? [R=301,L]
which got rid of the query string in the URL, but still creates a loop.
Ok, adding the 2nd RewriteCond finally fixed it - now rewriting correctly and no loop:
# redirect dynamic URL: ?state=arizona
RewriteCond %{QUERY_STRING} state=arizona [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule .* http://domain.com/arizona? [R=301,L]
# older rewrite rule that passes query string based on URL:
RewriteRule ^([A-Za-z-]+)$ index.php?state=$1 [L]
And here's the code to make it work for any state value, not just arizona:
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteCond %{QUERY_STRING} ^state=([A-Za-z-]+)$ [NC]
RewriteRule .* http://domain.com/%1? [R=301,L]
RewriteCond %{REQUEST_URI} ^/\?state=arizona$ [NC]
should be
RewriteCond %{QUERY_STRING} state=arizona [NC]
The request_uri ends at the ? token.
I tried many combinations but none works. I trying to write rewrite rule which:
Assume URL of http://example.com/project/big_bunny
When user visit site rewrite first try to read file /cache/project/big_bunny.html but only if there is no any get or post data
If there is no cache rewrite process standard router rule: index.php?_rt=$1 [L,QSA]
Any suggestions?
Try this:
# Check if using POST method
RewriteCond %{THE_REQUEST} !^POST
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
# Check if cache exists
RewriteCond /cache%{REQUEST_URI}.html -f
# Check if contains any GET query string
RewriteCond %{QUERY_STRING} !.+
# Do the rewrite
RewriteRule ^/(.*) /cache/$1.html [L]
# Check if already rewrote to cache
RewriteCond %{REQUEST_URI} !^/cache
RewriteRule ^/(.*) index.php?_rt=$1 [L,QSA]
Try this:
# Check the cache, and rewrite if file exists
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}.html -f
RewriteRule ^ /cache%{REQUEST_URI}.html [L]
# If URI doesn't start with /cache/, it wasn't cached so rewrite to index.php
RewriteCond %{REQUEST_URI} !^/cache/
RewriteRule ^(.*)$ index.php?_rt=$1 [L,QSA]
RewriteEngine On
RewriteRule ^/ai?$ /Market/publish.jsp [QSA,L,PT]
RewriteRule ^/ar?$ /Market/MailDispatch [QSA,L,PT]
RewriteCond %{HTTP_HOST} !^web\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://web.example.com/$1 [L,R]
#How skip www\. to web\. for this 1 ?
#RewriteRule ^/vi/?([0-9]+)\.htm$ /Market/vi.do?id=$1 [PT,L]
RewriteRule ^/li /Market/list.do [QSA,PT,L]
RewriteRule ^/vi/locations.jsp /Market/locations.jsp [PT,L]
ErrorDocument 404 /notfound.html
Nearly undoable(?) I try http://example.com/vi/{N}.htm should redirect to http://web.example.com/vi/{N}.htm where N is dynamic ID.
Seen mod_rewrite with subdomain and url pattern
There is no clear way to make eg http://example.com/vi/1096.htm pass up to next version http://web.example.com/vi/1096.htm where number is dynamic. I tried
A rule with the following scheme should do it:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^/vi/\d+\.htm$ http://web.example.com%{REQUEST_URI} [L,R=301]
It’s important to put this rule in front of those rules that do an internal redirect. Otherwise an already internally rewritten URL could be rewritten externally.
If you want to use this rule in a .htaccess file, remove the leading slash from the pattern in RewriteRule.
i'm deploying a ror application and now i have to rewrite the url (in apache) to
add a prefix www to the url
add / to the end of the url
So i took the following approach:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com/$1 [R=301,L]
The problem is that it is appending two trailing slash to my url
So for example a resource /question/ask are becoming:
http://foo.com//question/ask
I tried to add the following Rule before all my Rewrite rules to try to remove the double //:
RewriteCond %{REQUEST_URI} ^//
RewriteRule ([^/]*)/+(.*) http://www.foo.com/$1/$2 [R=301,L]
but it didnt work.. any idea to rip off all extras "//" added to the url?
The $1 will include a / at the beginning. You probably want
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^foo\.com
RewriteRule ^(.*)$ http://www.foo.com$1 [R=301,L]