mod_rewrite rule not working - mod-rewrite

I have the following rules in my htaccess:
RewriteRule ^([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=world [QSA]
RewriteRule ^([^/.]+)/([^/.]+)/?$ list.php?categoryShortForm=$1&locationShortForm=$2 [QSA]
RewriteRule ^([^/.]+)/([^/.]+)/[^/.]*-p([0-9]+)/?$ view.php?categoryShortForm=$1&locationShortForm=$2&postingId=$3 [QSA]
In my localhost (windows, xampp), it all works fine.
In my real server (linux, apache) the first 2 rules work fine, but not there 3rd one.
For example:
/plastic-surgery/california-usa/ works fine, but
/plastic-surgery/los-angeles-california-usa/test-1-p1 gives me a 404
Any idea??

Check to make sure that you can browse directly to the target URLs. If mod_rewrite is rewriting onto something that doesn't exist, you'll get that 404. It might help to ratchet up mod_rewrite's log level to a high value, so you can see what it's rewriting to.

I am a little unclear on how your working URL is supposed to actually work. All three of your patterns start with "one or more non-slash, non-period characters" ([^/.]+), but URLs going into the pattern matching start with slashes: "/plastic-surgery/california-usa/".
Have you turned on mod_rewrite logging and checked out what mod_rewrite is actually doing?
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

Related

URL Rewriting all subsites to main site

I'm new with URL Rewriting and dont know how to do this:
All sites after /ex1 should be redirect to index.php
My current script:
RewriteEngine On
RewriteRule ^(ex1|ex2)/?$ index.php [NC,L]
But this will only rewrite example.com/ex1/and not example.com/ex1/abcd
Thanks in advance ;)
Close to perfect, try this instead:
RewriteEngine On
RewriteRule ^(ex1|ex2)/? index.php [NC,L]
Note: the trailing $ in the regular expression has been removed. That one was the cause why only the exact urls example.com/ex1 and example.com/ex1/ were matched, but no longer ones. The trailing $ anchors the search to the line end, so the end of the url here. So if there is anything between the last matched characters (the ex1 or ex1/ in this case), then the expression will not match.
Another more general note: this pattern is only usable withing .htaccess style files, but not in the normal hosts configuration. There are situations when such files come in handy, for example if you do not have administrative control over the http server. But in general you should always prefer to place such things inside the http servers hosts configuration instead of using .htaccess style files. Reason is that such files are notoriously error prone, hard to debug and finally really slow the server down.

ModRewrite .htaccess / SEO URLs syntax

I am trying to re-use some .htaccess code on a new site/server and it doesn't work correctly. I'm not an expert with URL rewriting so would appreciate it if anyone can see if my syntax is incorrect or if there is something else I need to check server side.
I am using the following code:
Options +FollowSymLinks
RewriteEngine on
# News Pages
RewriteRule ^news/$ /news.php
RewriteRule ^news/(.*?)/$ /news.php?article=$1
It works for the 1st level, /news/ but /news/article-1/ just loads the /news/ (news.php) overview page. /news.php?article=article-1 works correctly.
Server is running Apache 2.2.9 and PHP is in CGI mode.
Try it such way:
RewriteRule ^news/(.*?)/?$ /news.php?article=$1 [L]
RewriteRule ^news/?$ /news.php [L]
[L] in the end of instruction stops server cheacking of other instructions, if current one match.
So you write more specific ahead of more general.
If your rules work correctly being the only one in the .htaccess, then such organisation of them can help.
/? - this means that / at the end can absent or present

MVC3 .net 4.0 extensionless urls and isapi_rewrite

Unfortunatly I am forced to use IIS6 for my MVC website. .net 4.0 adds functionality to mimic the stupid hack for getting extensionless urls to work in IIS6. My website is designed to take advantage url rewriting for SEO purposes. The keyword urls that the client wishes to use dictate an elaborate url rewriting scheme.
The problems with Microsofts impelmentation of the feature really comes down to url rewriting and the attempt to match a pattern. I have seen various rules that attempt to strip the dreaded eurl.axd from the url so that the patterns will match. I attempted to use these rules
RewriteRule ^(.)/eurl.axd/[a-f0-9]{32}(.)$ $1$2
or
RewriteRule (.)eurl.axd/. $1
which does work but it also introduces other problems when there are nested redirects. i.e. handling old urls to new ones, etc
what happens is the eurl.axd gets stripped and on the redirect the isapi_filter doesnt get the request which results in an IIS 404 errror.
Tinkering around with the urls, i came up with this possible solution.
RewriteRule ^generators/generator-parallel-capability/([^/])/([^/])$ /generators/htmlcontent/generator-parallel-capability/$1/$2 [NC,L]
it just grabs the eurl.axd portion and rewrites it to the executing url with it appended.
Is there a better way? I have several hundred urls that meet this pattern and it would be nice to have a single rule handle them all.
We used one generic rule on top of the config to cut the /eurl.axd234234
RewriteRule ^(.)/eurl.axd.$ $1 [NC]
this must work for everything but the root.
Using the rewrite rule RewriteRule ^(.)/eurl.axd.$ $1 [NC] results in some unplesant behavior when the url is rewritten more than one time. i.e. from an old url to the new vanity url then to the actual execution url.
Using maps, I was able to produce a pattern that works quite nicely and keeps the .htaccess file from being cluttered.
RewriteCond ${contentmap:$2} >"" [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /$1/${contentmap:$2}/$2/$3/$4 [NC,L]
RewriteCond ${contentmap:$2} >"" [NC]
RewriteRule ([^/]*)/([^/]*)$ /$1/${contentmap:$2}/$2 [NC,L]
The first detects the pattern /controller/some-content/eurl.axd/1234 and rewrites it to /controller/some-controller-action/eurl.axd/1234
the second does the same thing just without the eurl junk at the end. this is for my dev machine running iis 7
Im sure there are better ways and I am certainly open to better suggestions.

mod_rewrite understanding sub-directories whilst passing $_GET value server issue?

This may well be a server config issue; or simply a blindly obvious reason I'm missing...
Pre mod_rewrite URL:
www.example.com/subfolder/index.php?userName=x
The devised mod_rewrite:
RewriteEngine on
RewriteRule ^subfolder/[^/]([\w]*)$ /subfolder/index.php?userName=$1 [L]
It is my understanding that the above should allow navigation to: www.example.com/subfolder/x. However this causes a 404 error.
Rewrites without the sub-folder work fine; it is only when adding the subfoler to the mix things fall to put.
Your advice is much appreciated.
Try this one instead (works OK for me):
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/subfolder/index\.php$
RewriteRule ^subfolder/([^/]+)$ /subfolder/index.php?userName=$1 [L]
NOTE:
This rule is to be placed in .htaccess. If placed in server config / virtual host context, some small tweaking will be required.

how can I get my mod_rewrite to work?

I took a look around at other mod_rewrite questions and couldnt come up with an answer, so if anyone would point me to one that I may be duplicating I would appreciate it.
Anyways my question is: Why is my mod rewrite not working? I followed like 3 tutorials and spent a while reading forums and previous questions on this site, and couldnt get any of them to work. Ive tried doing it through my vhosts.conf file (I think its also known as the httpd.conf on some installations of linux) and through a .htaccess
Here is my .htaccess filed code
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^/article/([0-9]+)$ article.php?art_id=$1 [NC,L]
Here is the link im trying to get working: mysite.com/article?art_id=$1
To be honest I only have a slight clue as to how mod_rewrite works.
I have mod_rewrite installed (I don't manage the server, but the company I work for also has hosting services, thats how I have access to the vhosts.conf but I did not set the server up, and as far as I know the guy doing it is pro and set it up right)
I put this in to see if I could get it to work and it redirected me to my home page (I think that means its working)
# Options +FollowSymLinks
RewriteEngine on
RewriteRule ^fakepage\.html$ http://yoursite.com/ [R,L]
At least as far as your .htaccess file goes, using the rule you have will not work because the input to the RewriteRule will never have an initial forward slash on it. The rule should instead look more like this:
RewriteRule ^article/([0-9]+)$ article.php?art_id=$1 [NC,L]
However, what you have should have worked when you tried it in a virtual host (assuming you restarted the server), so I'm not sure if this is the actual issue. In what way is the rewrite not working? Are you getting 404 errors instead of being redirected to the file you were expecting?
Tim is correct about the slash at the beginning of your RewriteRule.
When trying to figure out Rewrites, it can be helpful to turn on logging:
RewriteLog "/var/www/mysite.com/rewrite.log"
RewriteLogLevel 9

Resources