first rewrite rule overriding the rest - apache

quick question for those who know a little more about mod-rewrite than i do:
I have some rules written in sequence like so:
RewriteRule ^(en|fr) index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/home index.php?page=home&lang=$1 [L]
RewriteRule ^(en|fr)/terms index.php?page=terms&lang=$1 [L]
however the first one seems to be over-riding the rest.
I've tried taking off the [L] but this doesnt do what I expected and it will never show the "terms" page.
Any ideas?

If you want http://www.example.com/en or http://www.example.com/fr/ (with or without trailing slash) to redirect to the first item, change the first rule to be:
RewriteRule ^(en|fr)/?$ index.php?page=home&lang=$1 [L]
The /? matches whether or not there's a trailing slash and the $ means "match the end of the URL" (don't match if the URL continues).

Related

Redirect directory with period to query strings in htaccess

I want to have personalized urls, where a person receives a mailer, that has their purl on it, and redirect them to a landing page that receives their name via query string
The url would be like
mywebsite.com/phx/joe.smith
I would like to redirect this traffic to
mywebsite.com/youngstown/index.php?first=joe&last=smith
This should be a 301 redirect, case insensitive. I'm able to do it if the directory was /phx/firstname/lastnaname, but I need it with the dot between first and last rather than a directory.
What I have so far is
RewriteEngine on
RewriteBase /
RewriteRule ^phx\/([^\/]+)\/([^\/]+)\/? youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx\/([^\/]+)\/? youngstown/index.php?FirstName=$1 [NC,R=301,L]
Any help would be much appreciated!
First, you don't need to escape slashes /. Apart from that, you're almost there. The main difference is \. vs /, e.g.
RewriteRule ^phx/(.+?)\.(.+)/?$ youngstown/index.php?first=$1&last=$2 [R,NC,L]
A complete solution could be:
RewriteEngine on
RewriteRule ^phx/([^./]+)\.([^./]+)/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]
RewriteRule ^phx/([^./]+)/?$ /youngstown/index.php?FirstName=$1 [NC,R=301,L]
As Olaf says, no need to escape slashes. I am matching here on "not a dot or slash". Also adding $ to delimit the end of the match and removing RewriteBase which is not needed.
Alternative Solution
Alternatively you could use a single rule which would set an empty param for LastName when not present:
RewriteEngine on
RewriteRule ^phx/([^./]+)(?:\.([^./]+))?/?$ /youngstown/index.php?FirstName=$1&LastName=$2 [NC,R=301,L]

Apache - rewrite rule for user folders

I'm trying to get a url like www.domain.com/user/myuser to forward to www.domain.com/user/index.html?user=myuser the code below does not work.
RewriteRule ^user/?$ /user/index.html?user=$1 [QSA,L]
does not work. is not helpful.
In this case your regex seems to be wrong. ^user/?$ means starts with user has an optional slash as the last letter
what you mean is
^user/(.*)$ which means match user/anything and redirect to /user/index.html?user=anything
so
RewriteRule ^user/(.*)$ /user/index.html?user=$1 [QSA,L]
should work

Url rewriting mod_rewrite with and without query string

I'm trying to rewrite from domain.com/page/soft-15/android-26/ to page.php?cat=15&os=26 with this code:
RewriteRule ^page/([0-9]+)\-([a-zA-Z0-9-]*)/([0-9]+)\-([a-zA-Z0-9-]*)$ page.php?cat=$1&os=$2
I thinks it works fine but, how can i rewrite domain.com/page/ and domain.com/page (without the last forward slash) to domain.com/page.php keeping both rules working?
ok think i got it mixing Jimp & Jon code
RewriteRule ^page/?$ page.php [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1&os=$2 [L]
RewriteRule ^page/(?:[a-zA-Z0-9-]*-)?([0-9]+)/?$ page.php?cat=$1 [L]
This math with
domain.com/page
domain.com/page/
domain.com/page/soft-15
domain.com/page/soft-15/
domain.com/page/15
domain.com/page/15/
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/soft-15/26
domain.com/page/15/26
and so on...
Add a trailing /? to your pattern (before the $ anchor). The ? makes the / optional.
Additionally, your patterns seem backward, matching the digits before the characters. Try this:
RewriteRule ^page/(?:([a-zA-Z\d-]*)-)?(\d+)/(?:([a-zA-Z\d-]*)-)?(\d+)/?$ page.php?cat=$1&os=$2
That should match these variations like these:
domain.com/page/soft-15/android-26
domain.com/page/soft-15/android-26/
domain.com/page/15/android-26
domain.com/page/15/android-26/
domain.com/page/soft-15/26
domain.com/page/soft-15/26/
domain.com/page/15/26
domain.com/page/15/26/
Your backreferences don't seem to jive with the example that you're using:
/page/soft-15/android-26/
to page.php?cat=15&os=26
Your regex looks like it's matching:
/page/15-soft/26-android/
And rewriting to:
page.php?cat=15&os=soft
If you're going by your example, you'd want something like:
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1&os=$2
RewriteRule ^page/[a-zA-Z0-9-]*?-([0-9]+)/?$ page.php?cat=$1
RewriteRule ^page/?$ page.php

skip next rule if pattern is matched

I have a rule that looks like this
RewriteRule ^([^/\.]+)/?$ index.php?marker=$1
I need to skip this rule if the url is /admin
I tried
RewriteRule ^admin/?$ /maps/admin [S=1]
followed by the first rule above but that is giving a The page isn't redirecting properly error.
Try another approaching by using RewriteCond.
RewriteCond {REQUEST_URI} !^admin/?$
RewriteRule ^([^/\.]+)/?$ index.php?marker=$1 [L]
Also, don't forget to add the [L] (Last) flag to your rules where appropriate. Otherwise it will continue processing them into one large compounded rule.

.htaccess same url with or without /

I am doing a painful rewrite of many urls on a website I am currently working on, but I have noticed a small problem:
RewriteRule ^domains/transfer$ ./cart.php?gid=11 [L,NC]
This line with navigate if I go to:
http://my-site/domains/transfer
But it won't work with a trailing /:
http://my-site/domains/transfer/
Is there a way I can change the RewriteCode to take either argument and go to the same page. It seems wasteful to have the same line twice to include a '/'
Any help would be appreciated.
Cheers
Change the line to this:
RewriteRule ^domains/transfer/?$ ./cart.php?gid=11 [L,NC]
The magic is here: /? and that allows for the preceding character, in this case the slash (/), to be optional.
If you want something to come after the transfer, then remove the dollar sign ($) which marks the end of the allowable matching.
I would recommend you to allow just one URL form, the one with or without the trailing slash, and redirect if malformed:
# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]