I am trying to redirect the following uris: search, search/acquia-search , search/acquia-search/, search/site or search/site/ to the homepage which is /. Here is my rewrite rule.
RewriteRule ^(search|search/acquia-search|search/site)(?!.*) / [R=301,L]
In the above rule I am trying to match those uris and ensure that they're not followed by anything using the negative lookahead.
here is my full .htaccess http://pastebin.com/stNgzfnD
what am I doing wrong?
Edit: The above did not work but the following did:
RewriteRule ^(search|search/acquia-search|search/site)/?$ / [L,R,NC]
(?!.*) negative lookahead doesn't make any sense since it will always fail the regex.
Better use this regex:
RewriteRule ^search(/(acquia-search|site/?)?$ / [L,R,NC]
Related
I am trying to redirect the following URL:
url/efx.aspx?xxxxxxx
to url/car-audio/efx-hardware/amp-install-kits
However it is redirecting whatever contains efx.aspx with the letters without the ? sign. I was wondering how I can fix this?
for example it is redirecting the following:
domain.com/efx.aspxlsdkjfhlasdf
but it is not redirecting
domain.com/efx.aspx?lsdkjfhlasdf
here is the .htaccess rule I wrote. how can I correct it?
RewriteCond %{REQUEST_URI} /efx.aspx[^/]+$
RewriteRule (.*) /car-audio/efx-hardware/amp-install-kits [R,L]
You can use this rule:
RewriteRule ^efx\.aspx$ /car-audio/efx-hardware/amp-install-kits? [R=301,NC,L]
Query string is not part of REQUEST_URI hence [^/]+ after efx.aspx fails your rule.
Also ? at the end of target URI removes any existing query string.
I am trying to rewrite any urls that end on a sequence of numbers and capitalized letters. Unfortunately I have only been partially successful. This is my .htaccess file:
RewriteEngine On
RewriteRule [A-Z0-9]+$ index.php?r=wizard/index&key=$1 [L]
For some reason, this only works for my urls that end on
key=QO106A654O65NN6828N
If the equal sign is missing from the url like this
keyQO106A654O65NN6828N
the url is not matched. I don't understand why that equal sign is significant since it's not even part of the regex. What could be causing this behavior?
As requested some example urls that don't work unless an '=' sign is inserted as described above:
/index.php?r=wizard/index&id&key4C2918IFIY3U4APKOI1
/index.php?r=wizard/index&id\xEF\xBF\xBD&keyQO106A654O65NN6828N
You cannot match QUERY_STRING using rewrite rule.
Have your rule like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)key([A-Z0-9]+)$
RewriteRule ^(index\.php)?$ $1?r=wizard/index&key=%1 [L,NE]
I have always struggled with regex, and after reading up on it for 45 mins my head is spinning. Negative lookaheads, what the...? (?:/(?:(?!s\d+).)*)+$<--- OMG!!!
:(
So, I have a rule
RewriteRule /([0-9]+) /?id=$1 [R]
and it works fine when the url is www.hi.com/123
How can I make it refresh to / (the document root i nthis case) if the url is www.hi.com/123abc or www.hi.com/a123bc?
I just want to make sure only urls with numbers and nothing else are matched.
I tried
RewriteRule /([0-9]+)([^a-z]+) /map.htm?marker=$1 [R]
But that refreshes towww.hi.com/?id=404, oddly enough.
To match 1 or more numbers in regex it will be:
[0-9]+
To match 1 or more numbers or letters in regex it will be:
[0-9a-zA-z]+
For your case RewriteRule rule will be:
RewriteRule ^([0-9a-z]+)/?$ /map.htm?marker=$1 [NC,L,R=302]
which will match /123abc OR /123abc/ OR /123 OR /abc/, note that trailing slash is optional. Flags I used are:
NC - Ignore Case
L - Last
R=301 - Use http status 302 for resulting URL
I would strongly suggest you reading mod_rewrite Reference doc: Apache mod_rewrite Introduction
However you also asked:
How can I make it refresh to / (the document root i nthis case) if the
url is www.hi.com/123abc or www.hi.com/a123bc?
That rule would be:
RewriteRule ^([0-9a-z]+)/?$ / [NC,L,R=302]
I am trying to setup my .htaccess file to do some nifty redirects for me.
Right now I have URLs like:
mysite.com/?video=1
I would like to have URLs like:
mysite.com/1/
Right now I have pieced together the following regex:
RewriteRule ^(.*)/?$ /index.php?v=$1 [L]
This works great if the URL is in the format
mysite.com/2
, but NOT if the format is
mysite.com/2/
, NOTE the trailing slash.
So what I really need help with is my regex! :)
Try to make the quantifier non greedy
^(.*?)/?$
otherwise the trailing slash is matched by the ., because its greedy and the explicit slash is optional.
Stema's idea should work. Or you could just make the regex more specific by e.g. only accepting numbers.
RewriteRule ^([0-9]+)/?$ /index.php?v=$1 [L]
or alpha numeric
RewriteRule ^([0-9a-zA-Z]+)/?$ /index.php?v=$1 [L]
What regexp should stand for "everything that does not follow 'index.php' should point to 'index.php/$1'" ?
For example, "http://mysite/moo" should point to "http://index.php/moo" while "http://index.php/moo" should not.
I am currently using PCRE regexp (Apache docs are saying that PCRE should be used here) taking no effect: RedirectMatch (?<=index\.php)(\/.+) /index.php/$1.
What is the right one?
UPD: the point is to use mod_alias only, ommitting mod_rewrite
You can use a RedirectCond with a non-matching pattern ("!" prefix). Try:
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) index.php/$1
Or:
RewriteCond $1 !^/?index.php
RewriteRule (.*) index.php/$1
Maybe:
^/?(?!index\.php)(.+)
i.e. a negative lookahead
I only tried with a simple redirect to a static site (works):
RedirectMatch ^(?!/index\.htm$).*$ /index.htm
Based on this, the following might help you (not tried myself):
RedirectMatch ^(?!/index\.php$)(.*)$ /index.php$1
See also: Mastering Lookahead and Lookbehind