Apache - rewrite rule for user folders - apache

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

Related

Trailing slash mod_rewrite rule for certain subfolders

I am trying to get a trailing slash mod_rewrite rule to work for only certain subfolders.
I have article URLs like ////. For example an article URL may be /news/role-playing-games/new-roleplaying-game-released/. Sometimes, for whatever reason, people try the URLs without the trailing slash and they get a 404. I would like instead for apache to add a trailing slash to only the URLs that match certain subfolders.
I tried this rule, but it does not work.
RewriteCond %{REQUEST_URI} ^/(news|reviews|tutorials|guides)/(.*)$
RewriteRule ^(.*)$ /$1/ [L,R=301]
Can anyone tell me what I am doing wrong?
Thanks in advance.
Try
RewriteRule ^((news|reviews|tutorials|guides)/[^/]+/[^/]+)$ $1/ [L,R=301]
I assumed that the url structure you gave is the only one.

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]

first rewrite rule overriding the rest

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).

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [R=301,L]

Rewrite WordPress permalink URL using mod_rewrite breaks Archive

The following mod_rewrite rule accomplishes the task of rewriting www.domain.com/2011/11/page to www.domain.com/page but breaks www.domain.com/2011/11/ (i.e. breaks WordPress Archive listings) and redirects it to the root of the site.
The rewrite rule should only rewrite items that have content after ^([0-9]{4})/([0-9]{1,2})/page but not ^([0-9]{4})/([0-9]{1,2})/.
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
Any recommendations?
ANSWER
The initial forward slash was missing at the beginning:
RewriteRule ^/([0-9]{4})/([0-9]{2})/(.*)$ /$3 [NC,R=301,L]
and the WordPress permalink entry needed:
/%postname%/
instead of:
%postname%
although I am not sure how much the latter helped.
I think you need to change the * to a +
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.*)$ /$3 [NC,R=301,L]
should be
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
With (.), it could possibly match nothing, thus a request like "/2011/11/" will match but the back reference for (.) will be blank, thus the rewrite goes to "/". The + indicates that there needs to be at least 1 character satisfying the "." in the regular expression.