Targeting single directory for rewrite rule - .htaccess

I have edited this question to use the actual URLs. I need the url
http://westernmininghistory.com/mine_db/main.php?page=mine_detail&dep_id=10257227
To be rewritten like
http://westernmininghistory.com/mine_detail/10257227/
I have tried
RewriteRule ^([^/]*)/([^/]*)/$ /mine_db/main.php?page=$1&dep_id=$2 [L]
Which works on this page but breaks every other page on the site. I was wondering if there was a way to force the rewriterule to only operate on files within the mine_db directory. I had tried RewriteCond but with no success:
#RewriteCond %{REQUEST_URI} ^/mine_db
I really don't know they proper syntax for this though. Any ideas?

First of your rule can be shortened and written without needing RewriteCond. Also it appears that you want to capture 2 variables after test_db.
You can try this rule instead:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC]
Which will work with URIs like /mine_detail/12345 (trailing slash is optional). Also note that above rewrite will happen silently (internally) without changing the URLi in browser. If you want to change URL in browser then use R flag as well like this:
RewriteRule ^(mine_detail)/([0-9]+)/?$ /mine_db/main.php?page=$1&dep_id=$2 [QSA,L,NC,R]

Related

URL Rewriting not possible?

I have a website with this url :
http://www.example.com/stage?dept=01
I want transform this url to this
http://www.example.com/stage-ain.html
I want that new url override the standard html and it's become the only url available (for SEO).
I do this, but it's not ok :
RewriteEngine On
RewriteRule ^stage?dept=01$ stages-ain.html [R=301]
Have you an idea ?
Thanks
The first argument of RewriteRule cannot match the query string. You have written a regex instead that matches the url stagedept=01 and stagdept=01.
You want to use a RewriteCond instead. You could use the %{QUERY_STRING} variable, but this will likely cause an infinite loop. Instead you probably want to match on %{THE_REQUEST}.
RewriteCond %{THE_REQUEST} /stage\?dept=01
RewriteRule ^ stages-ain.html [R,L,QSD]
Change the [R] flag to [R=301] when you have tested everything works as expected. Always use the L flag with external redirects, unless you have a very good reason to continue rewriting, as this can cause some weird problems.
See the documentation for more information.

Surprising rewriting of URL by htaccess rule

I've zeroed my problem and I've specific question.
With only the following code in the .httaccess why index2.php gets called if I type in my URL as www.mysite.com/url2 ?
RewriteEngine On
RewriteCond %{REQUEST_URI} (.html|.htm|.feed|.pdf|.raw)$ [NC]
RewriteRule (.*) index2.php [L]
I've also tested it at http://www.regextester.com and should not replace it with index2.php:
In the end I want this rule to skip any URL starting with /url2 or /url2/*.
EDIT: I've made screen recording of this problem: http://screenr.com/BBBN
You have this in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.html|.htm|.feed|.pdf|.raw)$ [NC]
RewriteRule (.*) index2.php [L]
What it does? it rewrites anything that ends with html, htm, feed , pdf , raw to index2.php. So, if you are getting results as your URL is ends with those extensions, then there are two possible answers:
There is another rewrite rule in an .htaccess in upper directories (or in server config files) that causes the URL to be rewritten.
Your URL actually ends with those extensions. have in mind, what you enter in your address bar, will be edited and rewritten. For example, if you enter www.mysite.com/url2 in your address bar and that file doesn't exist on server, your server will try to load the proper error document. So, if your error document is /404.html, it will be rewritten to index2.php at the end.
Update:
I think it's the case. create a file named 404.php in your document root. Inside your main .htaccess (in your document root), put this:
ErrorDocument 404 /404.php
delete all other ErrorDocument directives.
inside 404.php , put this:
<?php
echo 'From 404.php file';
?>
Logic behind it:
When you have a weird behavior in mod_rewrite, the best solution in my experience is using rewrite log. to enable rewrite log put this in your virtualhost or other server config directives you may choose:
RewriteLogLevel 9
RewriteLog "logs/RewriteLog.log"
be careful: the code above will enable rewrite log and start logging at highest level possible (logging everything). It will decrease your server speed and the log file will become huge very quickly. Do this only on your dev server.
Explanation: When you try to access www.mysite.com/url2, Apache gives your URL to rewrite module. Rewrite module checks if any of RewriteRules applies to your URL. Because you have one rule and it doesn't apply to your URL, it tries to load the normal file. But this file does not exit. So, Apache will do the next step which is showing the proper error message. When you set a custom error file, Apache will run the test against the new address. For example if error document is /404.html, Apache checks whether your rule applies to /404.html or not. Since it does, it will rewrite it.
The point to remember is apache will do this every time there is change in URL, whether the change is made by rewrite module or not!
The rule you list should work as you expect if this is the only rule. Fact is that theory is fun, but apparently it doesn't work as expected. Please note that . will match ANY CHARACTER. If you want to match the full stop/period character, you'll need to escape it. That's why I use \.(html|htm|feed|pdf|raw)$ instead of (.html|.htm|.feed|.pdf|.raw)$ below.
You can add another RewriteCond that simply doesn't match if the url starts with /url2, like below. This might not be a viable solution if there are lots of urls that shouldn't be matched.
RewriteCond %{REQUEST_URI} !^/url2
RewriteCond %{REQUEST_URI} \.(html|htm|feed|pdf|raw)$ [NC]
RewriteRule (.*) index2.php [L]
To get a better understanding of what is happening you can alter the rule to something like this. Now simply enter the urls you dont want to be matched in the url bar and inspect the url bar after the redirect happens. In the url-parameter you now see what url actually triggered this rule to match. This screencast shows you a similar version working with a sneaky rewriterule that is working away on the url.
#A way of finding out what is -actually- matched
RewriteCond %{REQUEST_URI} \.(html|htm|feed|pdf|raw)$ [NC]
RewriteCond %{REQUEST_URI} !/foo
RewriteRule (.*) /foo?url=$1 [R,L]
You can decide to match the %{THE_REQUEST} variable instead. This will always contain the request itself. If something else is rewriting the url, this variable doesn't change, meaning you can use this to overwrite any changes. Make sure the url won't be matching itself. You would get something like below. An example screencast can be found here.
#If it doesn't end on .html/htm/feed etc, this one won't match
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /.*\.(html|htm|feed|pdf|raw)\ HTTP [NC]
RewriteCond %{REQUEST_URI} !^/index2\.php$
RewriteRule (.*) /index2.php [L]

htaccess rewrite rule/condition gives file not found

This is what I am trying..
RewriteEngine On
RewriteCond $1 ^(.*)/__ver([0-9]+)__/(.*)$
RewriteRule ^(.*)$ /$1 [L]
What I am ultimately trying to do is version my file structure without actually changing the physical directory structure. I am also trying to set something up that will help prevent caching in some browsers. Either way I am basing this logic off of trying to remove the index.php from the URL with codeigniter but am failing.
I want to be able to in my html provide a uri in the src="" like
http://example.com/__ver333__/js/script.js
http://example.com/__ver333__/script.js
http://example.com/__ver333__/js/dir/script.js
or
__ver333__/js/script.js
__ver333__/script.js
__ver333__/js/dir/script.js
but as far as the server acts or is concerned I want it to remove the ver333 and pull the file accordingly. So its the equivilant of http://example.com/js/script.js or /js/script.js
RewriteCond $1 ^(.*)/__ver([0-9]+)__/(.*)$ does not mean anything. $1 is the first capture group in the regex in the first argument on that line. As this is the first argument, you are currently matching a file path against the characters $1. This will obviously never match.
What you want can easily be done with a regular rewriterule:
RewriteRule ^(.*)/__ver[0-9]+__/(.*)$ /$1/$2
Make sure that you enable the RewriteEngine and set RewriteBase /.

mod rewrite exclude all but php

Is it possible to edit htacces in such a way that only the following url is rewritten and the rest isn't?
http://www.example.com/index.php?x=foobar
to
http://www.example.com/foobar/
I want the pages not having x=... as a variable to behave normally
I got the following but that doesn't work
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/ index.php?x=$1
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav|txt)$
Who can help me?
First off, the RewriteCond must be put before the RewriteRule to which it belongs.
But I think that you need another approach for your case, something like this:
RewriteRule (.*)\.php - [PT,L]
RewriteRule ^(.*)/$ index.php?x=$1
The first rule Passes Through (PT) every PHP page, so the second rule is only applied to all non-PHP requests.
That second rule only applies to a "simple path", no matter if this path has a dot in it or not (e.g. hello.gif/ will match, too).
If this does not work for you, then you might consider one of these points to start further research:
the pattern ([^\.]*) matches everything that does not have a dot in it
see RewriteCond to skip rule if file or directory exists for RewriteConds where the following RewriteRule is only used if the request does not point to an existing file or directory
Hope this helps.

.htaccess Conditional Redirect When Pattern Does Not Match

I want to enable my users to enter search queries using a URL like this:
www.domain.com/searchterm
or with a trailing slash like this:
www.domain.com/searchterm/
However, I want to capture certain search terms and redirect them to an actual directory. For example, a query like this:
www.domain.com/css/site.css
should actually point to the CSS file, and should not pass "css/site.css" as the search term.
Here's my non working code:
RewriteRule ^/(.+)/?$ /index.php?search=$1 [L]
RewriteRule ^/css/(.+)$ /css/$1 [L]
This doesn't work - can anyone tell me what I'm doing wrong?
Instead of excluding all your existing urls it would be a far better solution to use a script like thia as a 404 page. While capturing the 404 you could still send a 200 response but atleast it would make your rewrite rules far easyer.
Or if you really want to do it without the 404, use this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /search_script [L]
It's not working because your expression isn't written well.
First, if you flip those two it should work fine. Second, take a look at your first RewriteRule. Your expression is ^/(.+)/?$. Basically, it's matching ANYTHING until the end of the string, so long as it starts with /.
If I were working on this file, I'd move the "search" RewriteRule to the end of the file. It would be interpreted last, and therefore it has less a chance of being used.
And as I'm writing this, I see Rick's option, which is what I would do even more than my own option. I'm not too familiar with the RewriteCond yet. What his does is it checks to see if the request is a file or a directory, and if not, it would then go make the search.
Cheers!

Resources