Clean URLs in IIS with mod_rewrite - iis

I have a mod_rewrite issue. Or more accurately, I have an issue with ISAPI_Rewrite, which is a mod_rewrite clone for IIS. Specifically, ISAPI_Rewrite v3 running on IIS 7. It aims to behave identically to mod_rewrite, so I hope my problem applies to mod_rewrite as well.
My problem boils down to this: I have a script bar.php which I want to access via the URL foo. I first rewrite foo to bar. Then try to convert the clean URL to a real file name.
RewriteRule ^foo$ bar
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
The RewriteCond is failing because ${REQUEST_FILENAME} still says "foo" rather than "bar". It is not updated after the first rewrite.
How can I make this work? I could fix this by simply writing RewriteRule ^foo$ bar.php. But I would strongly prefer to keep the URL remapping rules separate from the file extension rules.

Tentative answer...
RewriteCond %{DOCUMENT_ROOT}/$0.php -f
By using $0 I get access to the latest rewritten URL, which makes me happy. But I have to add %{DOCUMENT_ROOT} to turn that into a file name, which makes me sad. Bit of a kludge.

Related

how to rewrite SEO URL

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/files/section.php?id=1
Changed to:
demo.example.com/sample-section
I tried
Since you use .htaccess I assume you are using Apache. Here you'll find all relevant documentation.
First of all you need the mod_rewrite module to be installed (instructions to do so depend on the server's operating system and Apache distribution).
Then, the URL rewrite is pretty simple:
# First of all tell to mod_rewrite to operate.
RewriteEngine on
# Then, as many times you need, tell it on what to operate...
# For example: on files that do not exist. Or leave out RewriteCond to act on all.
RewriteCond "%{REQUEST_FILENAME}" !-f
# ...and what to do.
RewriteRule /sample-section "/files/section.php?id=1" [PT]
RewriteRule /another-section "/files/section.php?id=2" [PT]
The PT (PassThru) flag might be needed in some contexts, otherwise just use [L].

Redirect from asp to php not working

I'm trying to redirect all asp files to php files. At this point it doesn't matter if the parameters are lost. I tried the code below to accomplish this. But it seems to be ignored. If the asp page is default.asp, the url is rewritten to default.php, which doesn't exist.
I'm wondering if there is something on the server that is overriding my command or if the command is wrong?
RewriteRule ^.asp$ /index.php [R=301,L,QSA]
You are using anchors on both the ends of your URL match pattern. The ^ marks the beginning and $ marks the end. Since you only want to match URLs ending with .asp, use just $:
RewriteRule \.asp$ /index.php [R=301,L,QSA]
The rule you're looking for is:
# Does the file exist?
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# If so, rewrite the request
RewriteRule ^/?(.*)\.asp$ /$1.php [R=301,QSA]
The ^/? on there is because it's not clear from context whether you're doing this in your main config, or in a .htaccess file. I presume .htaccess since you've used that tag, but, just to be sure.

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.

Targeting single directory for rewrite rule

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]

.htaccess rewrite rule for /

I have a website where if I go to the URL http://mysite.com/community it shows page not found. But, the URL http://mysite.com/community/ correctly displays the page. How can I set up a rewrite for that "/" after community?
This is my present .htaccess:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
These were the ones tried by me, but failed
First,
RewriteRule ^(.*)/community $1/community/ [L]
second,
RewriteRule /community /community/ [L]
All with different combinations of with and without [L].
From the Apache URL Rewrite Guide:
Trailing Slash Problem
Description:
Every webmaster can sing a song about the problem of the trailing slash on URLs referencing directories. If they are missing, the server dumps an error, because if you say /~quux/foo instead of /~quux/foo/ then the server searches for a file named foo. And because this file is a directory it complains. Actually it tries to fix it itself in most of the cases, but sometimes this mechanism need to be emulated by you. For instance after you have done a lot of complicated URL rewritings to CGI scripts etc.
Solution:
The solution to this subtle problem is to let the server add the trailing slash automatically. To do this correctly we have to use an external redirect, so the browser correctly requests subsequent images etc. If we only did a internal rewrite, this would only work for the directory page, but would go wrong when any images are included into this page with relative URLs, because the browser would request an in-lined object. For instance, a request for image.gif in /~quux/foo/index.html would become /~quux/image.gif without the external redirect!
So, to do this trick we write:
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R]
The crazy and lazy can even do the following in the top-level .htaccess file of their homedir. But notice that this creates some processing overhead.
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]
Well, after trying out all the above solutions as well as some of my own, I finally solved this. I'm definitely sure that this is NOT a complete solution but it sure solved it for the time being.
Solution: Just created an empty directory named "community" in the root folder. That's it!
But I'm still on the lookout for the actual solution to this.

Resources