htaccess make rewrite condition page specific - .htaccess

I have the following in my htaccess which works fine for the page called portfolio:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/portfolio#!%1? [R=301,L,NE]
However I now need to keep this and also produce this same rewrite for another page called PrivateGallery.
How can I do this without the two clashing, is there any way to write the first condition line differently for example so that it was page specific and then copy this a second time changing the page names accordingly?
Thanks in advance

I've eventually found what I was looking for after countless experiments. I achieved it with this:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* %{REQUEST_URI}#!%1? [R=301,L,NE]
So both my pages containing escaped fragments will rewrite to their hashbang representative URLs without one page also redirecting to another.
This was mainly needed so that I could get Facebook to parse these URLs for a project where I needed like button/fb comments to represent separate image URLs in the document.

Related

htaccess redirect dynamic urls

I am trying to write a htaccess redirect, but it is not working as I want it to.
My current (working) htaccess redirects all html pages with 1 character to the index file as:
RewriteRule ^([a-zA-Z0-9]).html$ index.php?letter=$1
So a.html gets redirected to index.php?letter=a
Now I need to redirect the page a.html?page=2 to index.php?letter=a&page=2
Basically I want to redirect the url, but leave the dynamic part intact.
All of the following return a 404:
RewriteRule ^([a-zA-Z0-9]).html?page=([0-9]+) index.php?letter=$1&page=$2
RewriteRule ^([a-zA-Z0-9]).html?page=(.*) index.php?letter=$1&page=$2
RewriteCond %{QUERY_STRING} page=(.*)
RewriteRule ^([a-zA-Z0-9]).html(.*) index.php?letter=$1&page=%1
I think I'm close, but I can't seem to get there :/
could anyone give me the last push?
Your RewriteRule needs to be
RewriteRule ^([a-zA-Z0-9])\.html$ index.php?letter=$1 [QSA,NC,L]
Please, note that URL parameters are not available for matching within the RewriteRule. If you simply need to append an extra URL parameter you can do so along with the [QSA] flag which would take care of appending the original URL parameters for you.
Please, note that the dot before html needs to be escaped \. as well. The [L] makes sure that rewriting stops and no further rules (if any below) are applied.

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]

Redirect when two strings are matched

Trying to make the url:
www.google.com/forum.php?fid=5
Redirect to:
www.google.com/new.php?fid=5
But also need it to keep everything else intact because for example the link can be:
www.google.com/forum.php?fid=5&sortby=asc
And need sortby portion to be there upon redirect.
What the redirect needs to do is look for forumdisplay.php and fid=6 and when both are found in the same url it redirects to blog.php and removes fid=6 but keeps any other parameters there.
I searched and found how to do it with one string but not two.
Also, what's the difference between redirect and rewrite?
This is related to MyBB forum software. I made a separate php file that uses forumdisplay but with a new name.
Using mod_rewrite you could use a condition to verify the id and grab what comes after if anything:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/forumdisplay.php
RewriteCond %{QUERY_STRING} ^fid=6(&.*|.*)
RewriteRule ^.*$ /blog.php?%1 [R=301,L]

Can I create a search engine friendly URL from this custom ColdFusion CMS URL?

I have inherited a custom ColdFusion CMS app. The URL's that it creates are horrendous. Not at all suitable for SEO or readability for that matter. An example of a URL in this CMS is:
http://www.mysite.com/Index2.cfm?a=000003,000010,000019,001335
Basically, each level of hierarchy is stored in the database based upon that long string of comma separated values. So in the case of the example I used, that particular page is 4 levels deep in the CMS hierarchy.
Basically what I would like to see is a format similar to this
http://www.mysite.com/level-1/level-2/level-3/level-4
Is this possible? Any help would be greatly appreciated. For what it's worth we are using ColdFusion 6 at present time, but will be upgrading to 8 in the near future.
First of all, are you willing to have the index.cfm in the URL? Like: http://www.mysite.com/index.cfm/level-1/level-2/level-3/level-4 ? If not, then you'll need to be doing a rewrite to remove the index.cfm, but still allow CF to process the page. Your .htaccess would look something like this:
RewriteEngine On
# If it's a real path, just serve it
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# Redirect if no trailing slash
RewriteRule ^(.+[^/])$ $1/ [R=301,L]
# Rewrite URL paths
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ /index.cfm%{REQUEST_URI} [PT]
Next step, you'll need to "catch" the URLs and serve up the correct pages based on the SEO-friendly URLs. You can grab the incoming URL from the CGI.path_info variable. It's hard to know what your code should look like without knowing how it currently processes those URL variables, but essentially you'd have some kind of mapping function that grabbed the SEO-friendly names and substituted in the numbers to grab the content.
The third step is rewriting any URLs that are generated by your CMS to output the SEO-friendly URLs. Same mapping happens here, only in reverse.

Is my htaccess file causing multiple calls to one page?

I think my htaccess file that makes use of mod_rewrite is causing my pages to be called more than once. Can anyone see if this could happen with my current htaccess file? Or if there is even a possibility? This happens in the view.php page only (from what I have seen).
# REWRITE DEFAULTS
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^view\.php$ /%2? [L,R=301]
# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^([0-9a-z]+)$ view.php?t=$1 [L]
What is happening in my PHP scripts is that they are being called more than once or at the very least a function is being called more than once even though I have made sure its being called once!
Thanks all
Those mod_rewrite conditions and rules will not cause a script to be called more than once. The rules themselves can be called multiple times. Every time a URL is successfully rewritten into a new request, the new request will invoke the rules again. However, this will stop as soon as a "real" resource (script, webpage, etc.) is identified and retrieved a single time.
Are there other references on your page that would make another request? For instance an IMG tag will cause a browser to make another request. Those requests will cause the rules to be run again. It looks like something with a dot (e.g. picture.jpg) will not match your rules, but something else might.
Other things to look for are CSS and scripts that are referenced.
Without reading your pasted code, I want to say no. the htaccess runs through each line and stops at the first rule that matches the request

Resources