How mod_rewrite can add subdomain? - mod-rewrite

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.

Related

Rewriting URLs with one query string to another URL with a different query string

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]

mod_rewrtie remap and redirect

what i am trying to do is this:
If url http://example.com/foo/foo.php is called it should be redirected with 301 to http://example.com/bar/foo.php
AND
if url http://example.com/bar/foo.php is called it internally calles the /bar/foo.php script but browser url is not changed (remap).
my rules look like this
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [PT]
But this gives me too many redirects error.
Each rule activated separately works but together they seem to conflict...
You can either match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo/foo.php
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [L]
Or prevent rewrite looping altogether:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^foo/foo.php(.*) bar/foo.php$1 [R=301]
RewriteRule ^bar/foo.php(.*)$ foo/foo.php$1 [L]

apache mod rewrite with the_request

I want to do the following with apache (mod rewrite).
if the user requests http://hostname.tld/index.php/folder/subfolder i want it to redirect (with a R=301) to http://hostname.tld/folder/subfolder.
if the user requests http://hostname.tld/folder/subfolder the request should internally be rewritten to index.php/folder/subfolder.
To prevent an endless redirect the first rule should check for %{THE_REQUEST}. The problem here is that I am unable to append "folder/subfolder" with a regex. How should I do this?
For the second rule I have this (and seems to work).
RewriteCond %{HTTP_HOST} hostname.tld [NC]
RewriteRule ^(.*)$ index.php [QSA,L]
The first one is still a problem.
I think the first one should be something like
RewriteCond %{THE_REQUEST} (.*)index.php(.*) [NC]
RewriteRule /index.php/$ http://hostname.tld/$1 [R=301,QSA,L]
But that is not really it.
The first should be.
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^index.php/(.*)$ http://hostname.tld/$1 [R=301,L]
I also see that your second rule redirects http://hostname.tld/folder/subfolder to http://hostname.tld/index.php (not http://hostname.tld/index.php/folder/subfolder). But as long as that works it's fine, as this it also prevents the redirect loop.
But just in case, here is the solution to add the folder/subfolder part:
RewriteCond %{HTTP_HOST} ^hostname\.tld$ [NC]
RewriteCond $1 !^index\.php
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Cannot able use Multiple rule in mod_rewrie

I need used use it
RewriteEngine On
RewriteCond %{SERVER_PORT} !^9090$
RewriteRule ^vendor/([0-9]+).html$
productcategory.iface?Operation=category&catid=$1 [L,R]
RewriteRule ^vendor/([0-9]+)([0-9]+).html$
productcategory.iface?Operation=product&prodid=$2
RewriteRule ^vendor/([0-9]+)([0-9]+)([0-9]+).html$
productcategory.iface?Operation=vendet&prodid=$2&venid=$3
But I Used this rule i found Error on 500 that is misconfiguration error
Line breaks inside the directives are not allowed. So try this:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^9090$
RewriteRule ^vendor/([0-9]+)\.html$ productcategory.iface?Operation=category&catid=$1 [L,R]
RewriteRule ^vendor/([0-9]+)\([0-9]+)\.html$ productcategory.iface?Operation=product&prodid=$2
RewriteRule ^vendor/([0-9]+)\([0-9]+)\([0-9]+)\.html$ productcategory.iface?Operation=vendet&prodid=$2&venid=$3

How do I write a custom mod_rewrite .htaccess file for CakePHP routing?

I've rewritten my web app using CakePHP, but now I need to have my old formatted urls redirect to my new url format. I can't seem to add my own custom mod rewrite rule. I've added it above the main cakephp rewrite rule, but I'm getting an infinite redirect loop. I just want http://mysite.com/index.php?action=showstream&nickname=user to redirect to http://mysite.com/user before the cakephp rewrite happens.
EDIT: Ok, so now when the condition is met it's redirecting but it's appending the original query string to the end. I'm assuming that's due to the QSA flag in CakePHP rewrite rules, but I was under the impression the "L" in my rule would stop that from executing...
RewriteEngine On
RewriteCond %{QUERY_STRING} ^action\=showstream&nickname\=(.*)$
RewriteRule ^.*$ http://mysite.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When you do a capture inside the RewriteCond line instead of the RewriteRule, you have to reference the capture with %N instead of $N. That is, your RewriteRule line should be:
RewriteRule ^index.php$ /%1 [R=301,L]
Try to test the request line (THE_REQUEST) to see what URI originally has been requested:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php
RewriteCond %{QUERY_STRING} ^action=showstream&nickname=([^&]*)$
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
But maybe it would be easier to do this with PHP.

Resources