Simple modrewrite rule with regex - mod-rewrite

This are my urls
http://test.aDomain.com/x-61bff
http://test.aDomain.com/x-ssmJhs54as65d4
http://test.aDomain.com/x-115545454
my rewrite rules
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule ^create-account create-account.php [L]
RewriteRule ^account account.php [L]
RewriteRule ^impress impress.php [L]
RewriteRule ^(x\-[a-zA-Z0-9]+) redirect.php?code=$1 [L]
The result is
http://test.aDomain.com/redirect.php
But why? it should be
http://test.aDomain.com/redirect.php?code=x-61bff
i dont get it ... can anybody help?

mainly because you got the regex wrong:
the / between the host-name and the path is really part of the path (e.g. it is /x-61bff instead of x-61bff). however, your regex matches only an x at the very beginning, thus /x-61bff will never match.
the minus-sign has only a special meaning within square brackets (e.g. [A-Z]); outside it is just another character, there is no need to escape it
so your rewrite-rule really should look like:
RewriteRule ^/(x-[a-zA-Z0-9]*) redirect.php?code=$1 [L]

Related

RewriteRule negate (NOT) not working

I am trying to do the following redirect:
any url such as:
/about/apple/1
/about/green
/about/apples
/about/pears/1
to:
/about/apple
using the following, which does not work:
RewriteRule ^about/(!apple) /about/apple [R=301,L]
The following test line works fine:
RewriteRule ^about/apples /about/apple [R=301,L]
I have tried:
RewriteRule ^about/!(apple) /about/apple [R=301,L]
RewriteRule ^about/(?!apple) /about/apple [R=301,L]
RewriteCond %{REQUEST_FILENAME} !(about/apple)
RewriteCond %{REQUEST_FILENAME} about(.*)
RewriteRule . /about/apple [R=301,L]
And also tried putting it into a rewritecond before hand, but nothing works.
Thanks.
Your regular expression pattern ^about/(?!apples) matches none of the uris you want to redirect. It matches /about/foobar but does not match /about/apples or /about/apples/foobar as you have negated the apples substring in the pattern.
You can use
RewriteRule ^about/apples/(.*)$ /about/$1 [L,R]
This will redirect /about/apples/ or /about/apples/foobar to /about/foobar .
Make sure to clear your browser cache before testing this.

Relative path and mod_rewrite issue

Have a trouble with mod_rewrite.
I want :
htp://example.com/a/some_text -> htp://example.com/?p1=some_text and
htp://example.com/b/some_text -> htp://example.com/?p1=some_text
So, I type:
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^(.*)$ ?fsearch=$2 [QSA]
and get wrong relative paths in css, such as htp://example.com/a/CSS/main.css instead of htp://example.com/CSS/main.css. And get nothing in $2 too.
Help, please
$2 tries to pick the second capture group of the RewriteRules subject, but there is only one! You probably mean %2 which picks from the RewriteCond...
Since you only append a query string in your RewriteRule the original URI (path) is left untouched.
You can simplify the pattern in the RewriteRule since you are not interested in the subject anyway.
This is probably what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /?fsearch=%3 [QSA]
Much better however would be to rewrite directly to whatever script you actually want to process the request to safe another rewriting round to find the index document, so something like:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%3 [QSA]
And unless you decide to use the first capture group in the RewriteCond you can also simplify that further:
RewriteEngine on
RewriteCond %{REQUEST_URI} \/(a|b)\/(.*)$
RewriteRule ^ /index.php?fsearch=%2 [QSA]

URL Rewrite always taking the first condition

RewriteRule ^list lists.php [NC]
RewriteRule ^list/([^-]+)/ lists.php?listid=$1 [NC]
The problem is even if the link is like example.com/list/5 it is directed to list.php without the value. The second condition is not getting satisfied ever. How to solve this?
Try
RewriteRule ^list$ lists.php [L]
RewriteRule ^list/([\d]+)/$ lists.php?listid=$1 [L]
Notice that it will match list/5/ but not list/5. That would be:
RewriteRule ^list/([\d]+)$ lists.php?listid=$1 [L]
Edit: You can use [NC] if you want... I personally like the [L] flag and the case wording respected.

How mod_rewrite can add subdomain?

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.

mod_rewrite trailing slash with RewriteCond

I have searched through the related mod_rewrite qustions but I can't anything specific enough so I'll post:
Heres my rule that adds a trailing slash:
RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [R=301,L]
Only I want to exclude one specific directory eg. /mydirectoryname/ and not add the trailing slash to anything that starts with that. Reason being its breaking some of my ajax calls.
Add another RewriteCond:
RewriteCond %{REQUEST_URI} !/mydicrectoryname)/
You can descibe that with just one condition:
RewriteCond $0 !^mydirectoryname(/|$)
RewriteRule ^[^\.]+[^/]$ /$0/ [R=301,L]

Resources