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]
Related
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]
RewriteRule ^categories/([A-Za-z0-9\-]+)/?$ /categories.php?c=$1 [QSA,L]
This is my RewriteRule, it deals with categories such as /categories/Family perfectly that URL displays the page as I would like it too, However with something such as /categories/Web%20Design I get an The requested URL /categories/Web Design was not found on this server.
This is a pain, I've even tried to use a space in the ReWriteRule after the 9 in [A-Za-z0-9-], what's the best way to handle spaces in URLs with the rewrites?
Thank You All.
Use this rule by including space in your character class:
RewriteRule ^categories/([A-Za-z0-9\s-]+)/?$ /categories.php?c=$1 [QSA,L]
try adding \s to the Regex...
That matches whitespace.
([A-Za-z0-9\-\s]+)
I want to redirect URL
domain/Family_He..
to
domain/Family_Health_insurance
using RewriteRule. I have tried with
RewriteRule /Family_He(.*)$ /Family_Health_insurance
and it is working. But I have some more page with urls like
domain/Family_Health_info
domain/Family_Health_quote
domain/Family_Health_child etc
When I tried as
RewriteRule /Family_He\.\.$ /Family_Health_insurance
then this won't works for me. Please help me out.
Are you aware that there are actually two spaces at the end of your subject string that would prevent \.$ (a literal dot at the end of the string) from matching?
To have it redirect (302) you need to add the R flag. To match the dot you need to escape it using \. like
RewriteRule ^/Family_He\.\.$ /Family_Health_insurance [R=302,L]
Note that the leading slash doesn't work in htaccess, only in httpd.conf
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
I'm almost there with a mod_rewrite rule, but I've caved in :)
I need to rewrite
country/[countryname].php
to
country/[countryname]/
however, [countryname] may have an underscore like this: 'south_africa.php' and if it does I want to replace it with a hypen: 'south-africa/'
I also want to match if the country has numbers following it: 'france03.php' to 'france/'
Heres my rule, its almost there but its still adding a hyphen even if there is no second part after the underscore.
RewriteRule ^country/(.*)_(.*?)[0-9]*\.php$ country/$1-$2 [R=301,L]
so currently 'country/south_.php' becomes 'country/south-/'
Can someone please help me find the missing piece of the puzzle? Thanks.
Try this:
RewriteRule ^country/([^_]*)_([^_]*?)\d*\.php$ country/$1-$2 [R=301,L]
This rule will match urls with a single underscore - you'll need a different rule for more underscores or none.
If you want to make sure $2 contains only letter and isn't empty, change ([^_]*?) it to ([a-zA-Z]+).
Alternatively you could do it over several passes:
# If request is for something in "country/"
RewriteCond %{REQUEST_URI} ^country/.+\.php$
# Replace underscore and digits with (single) hyphen
RewriteRule [_0-9]+ \-
# Remove extension (and possible trailing hyphen)
RewriteRule ^(.*)-?\.php$ $1
# Final rewrite
RewriteRule ^country/(.*)$ country/$1 [R=301,L]
Untested ... and not necessarily "pretty" :)