Why doesn't my mod rewrite rule work? - .htaccess

I am trying to beautify a url to go from something ugly like:
www.domain.com/lookup.php?firstname=jack&middlename=ron&lastname=paul
to
www.domain.com/n/jack/ron/paul
I have some other rewrites working on the server, but this one doesn't seem to like me as I get a 404. Here is what I'm trying:
RewriteRule ^/n/(.*)/(.*)/(.*)$ lookup.php?firstname=$1&middlename=$2&lastname=$3
What am I missing? Why doesn't that work?
Something more simple like this works fine:
RewriteRule ^article/(.*)$ articles.php?article_id=$1
What more needs to be done to do one more complex?

This isn't working because you have rules in an htaccess file. When URI's are processed by rewrite rules in an htaccess file, the leading slash (or the "prefix") is always stripped. That means:
# here-------v
RewriteRule ^/n/(.*)/(.*)/(.*)$ lookup.php?firstname=$1&middlename=$2&lastname=$3
The leading slash in your pattern causes it to never match, since all URI's in an htaccess file have it stripped off. Rewrite rules in an htaccess file is essentially like rules within a <Directory> container. Try getting rid of that slash.

I'd have to see more of your httpd config but here are a few notes:
You don't seem to have [L,R] or [L,R=301] or [L,R=302] at the end of your rule (the L if this is the Last rule in the segment which I suspect it is).
Your .* regular expressions may be too greedy -- not sure if they will match like you want them to.

Related

Difficulty Rewriting URL using .htaccess

I've seen several answers but none of them have worked for me.
How do I rewrite the below using my .htaccess? mod_rewrite is on in my file.
http://example.com/local/vendor/index.php?handle=Company
to be:
http://example.com/local/vendor/Company
How do I rewrite the below...
There's possibly a more fundamental problem here. You don't rewrite it that way round in .htaccess. You rewrite from the "friend" URL back to the actual URL/filesystem path. So, it's the other way round, you rewrite from:
http://example.com/local/vendor/Company
to the actual URL your application is expecting:
http://example.com/local/vendor/index.php?handle=Company
For Example:
RewriteEngine On
RewriteRule ^(local/vendor)/(Company)$ /$1/index.php?handle=$2 [L]
$1 and $2 are backreferences to the captured groups in the RewriteRule pattern.
The above matches just the specific URL you stated, ie. /local/vendor/Company. I suspect that "Company" is intended to be a placeholder? In which case you need to make this more general. For example:
RewriteRule ^(local/vendor)/(\w+)$ /$1/index.php?handle=$2 [L]
\w is a shorthand character class for word characters. This excludes the dot (ie. .) so avoids a rewrite loop when rewriting to index.php.
UPDATE: The above assumes your .htaccess file is located in the document root, ie. example.com/.htaccess. However, if the .htaccess file is located in the /local subdirectory (ie. example.com/local/.htaccess) - as you appear to suggest in comments - then you will need to adjust the above directives to remove local/ from the RewriteRule pattern and remove the slash prefix from the substitution. For example:
RewriteRule ^(vendor)/(\w+)$ $1/index.php?handle=$2 [L]

my .htaccess rewriteRule is failing

I'm having trouble understanding why this rewrite isn't doing what its told.
NOTE: the first rewrite in my .htaccess file works properly so its not a problem with using mod_rewrite on local host.
i have URIs which i know will be in the format:
http://localhost/managerhub/my-manager.php?i=1&t=dashboard
when site goes live:
http://themanagerhub.com/my-manager.php?i=1&t=dashboard
my .htaccess file reads thus:
RewriteEngine On
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+) my-manager.php?i=$1&t=$2 [PT]
To achieve clean URls like:
http://localhost/managerhub/my-manager-1-dashboard
Ideally i dont really want the first capture group ([0-9]+) since i dont really want the 'i' value in the resultant
clean url - so ideally id like:
http://localhost/managerhub/my-manager-dashboard
However ive not even got the rewrite to work so far at all having tried:
leading forward-slash on the target (though i dont think it was necessary)
tried changing the '&' ampersand in the target to use &
removing the [PT] passthru flag replaced with and without [L] flag
tried most 'least' restrictive character classes in the pattern i.e. (.*) instead of ([0-9]+)
commented 'out' the first RewriteRule which works flawlessly BTW - so using the troublesome rule in isolation
Non of these have worked - the second rewrite rule has no effect on the target urls so i cant even see were the discrepancy is. I'm still new to mod_rewrite so sort of rely on an informative fail so i can work out were my reg-ex is wrong but i suspect its just being ignored since im getting 'zilch' back!!
Any help appreciated - maybe with a pointer to my folly.
thanks
Your htaccess file, I'm assuming, is in the "managerhub" directory. That's where those rules need to be. You may need to add a base as well:
RewriteEngine On
RewriteBase /managerhub/
RewriteRule ^([a-z\-]+)$ $1.php [L]
RewriteRule ^my-manager-([0-9]+)-([a-z]+)$ my-manager.php?i=$1&t=$2 [L,PT]
which you'd need to change when they get to the live site. You can remove the first capture group via:
RewriteRule ^my-manager-([a-z]+)$ my-manager.php?t=$1 [L,PT]
The L flag isn't exactly 100% needed.

Why doesn't this URL rewrite work?

This is a page on my domain: www.mydomain.com/en/stats.php
I want it to look like this: www.mydomain.com/en/statistics/players
This is pretty simple to accomplish, but for some reason the htaccess code below doesn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/statistics/players stats.php [R=301,L,QSA]
I get a 404 error when I try to open the SEO-friendly version. The page opens fine when I use the regular URL. What am I doing wrong?
The URL might not have a leading slash. Try without or optional slash. Additionally, you must check for the leading en, when you anchor your pattern at the beginning
RewriteRule ^/?en/statistics/players /en/stats.php
What worked in the end is the following:
RewriteRule en/statistics/players en/stats.php [NC,L]
I think it needs to be:
RewriteEngine On
RewriteRule ^en/statistics/players en/stats.php [QSA]
The R=301 means it will perform a redirect instead of a rewrite and try to redirect to /stats.php which doesnt exists I guess.
The L flag means that this is the last rewrite rule that will be processed for this request, which in this case I doubt is what you want. If there are rewrite rules further down for /en/stats.php they wont be processed.

mod_rewrite rule breaks when adding trailing slash 2 subdirectories deep

I'm very, very new to mod rewrite rules, and having trouble with one I'm using to redirect a site previously hosted in a subdirectory to a new domain. Here's the rule I'm using:
RewriteEngine on
RewriteBase /oldsite/ #also tried this without the trailing slash
RewriteRule ^.* http://newsite.com [R=301,NC,L]
This works perfectly until you get 3 levels deep and add a trailing slash to the redirected url. So, the results look like this:
olddomain.com/oldsite redirects to newsite.com [CORRECT]
olddomain.com/oldsite/ redirects to newsite.com [CORRECT]
olddomain.com/oldsite/subdirectory redirects to newsite.com/subdirectory [CORRECT]
olddomain.com/oldsite/subdirectory/ redirects to newsite.com [INCORRECT!]
I feel like I'm 99% there, but pulling my hair out a bit figuring out that last little bit. Any idea what I need to change?
Thanks!
Also, I tested placing the .htaccess file in the public html folder as well as in the /oldsite folder and there are no other htaccess files elsewhere on the site.
You're almost there, put this in the .htaccess in /oldsite on your server:
RewriteEngine On
RewriteRule (^.*) http://newsite.com/$1 [R=301,NC,L]
When you put the regex to match in parenthesis, it becomes available to use as $1. If you have more than one, $1 is the first, $2 the second, etc.
I suggest reading the manual page for mod_rewrite, specifically the rewriterule section (http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule).

mod_rewrite rules not working as expected

I am using the following code
RewriteEngine on
RewriteRule ^/(certain|folders|on|server) - [NC,L]
RewriteRule (.*) http://newserver/blog$1 [L,NS]
I would expect it to ignore my list of folders, and redirect to everything else. Instead, it seems to always be redirecting to "newserver". Changing the "-" to a URL in the first statement does work, but I don't actually want those folders directed anywhere.
Why does my statement not work as expected?
Also, I have noticed that for all the folders not listed, I have "newserver/", while the ones I listed are just "newserver" (no uri)
You need to remove the leading slash in your rule. Rewrite rules in an htaccess file are given a URI to match against with the leading slash stripped off. Change your first rule by adding a ? after the slash:
RewriteRule ^/?(certain|folders|on|server) - [NC,L]
Or you can change it into a condition:
RewriteCond %{REQUEST_URI} !^/(certain|folders|on|server)
that will be applied to the rule that follows it.
Turns out my rules do work as expected. I was previously trying using these rules in apache.conf. Moving it to the sites "VirtualDirectory" tag seemed to do the trick

Resources