Change to URL and ReWrite rule not working - .htaccess

Have recently been making use off the htaccess file to create rewrites.
Everything works well, but have made a recent change to include a category name followed by the product, like this:
http://localhost/Limestone_Tiles/products/Antalya_Blanc.html
It just used to be:
http://localhost/products/Antalya_Blanc.html
When everything worked ok. The rewrite rules where as follows:
RewriteRule ^products/(.*).html$ product_details.php?&furl=$1 [NC,L]
RewriteRule ^products/images/(.*)$ images/$1
RewriteRule ^products/picture_upload/(.*)$ picture_upload/$1
RewriteRule ^products/thumbnail.php(.*)$ thumbnail.php$1
So to accommodate the new change I have changed the rewrite rules as follows:
RewriteRule ^(.*)/products/(.*).html$ product_details.php?&furl=$1 [NC,L]
RewriteRule ^(.*)/products/images/(.*)$ images/$1
RewriteRule ^(.*)/products/picture_upload/(.*)$ picture_upload/$1
RewriteRule ^(.*)/products/thumbnail.php(.*)$ thumbnail.php$1
Now this kind of works because
http://localhost/Limestone_Tiles/products/Antalya_Blanc.html
Will load, but none of the images do - which does not make sense to me when the page does? Why don't the images?
If I change the rewrite rule to:
RewriteRule ^Limestone_Tiles/products/images/(.*)$ images/$1
The images will load for that particular category!
If I put the image url into the browser:
http://localhost/Limestone_Tiles/products/images/face.png
I get the follow message:
The requested URL /images/Limestone_Tiles was not found on this server.
Which is also odd as /images/Limestone_Tiles isn't the Url I have just pasted into the address bar.
Perhaps my rewrite rule is incorrect still?
Many thanks for your help in advance.

This is because your rules have groupings that backreference the first grouped match and not the match you really want. Say, given this rule:
RewriteRule ^(.*)/products/(.*).html$ product_details.php?&furl=$1 [NC,L]
And this URL:
http://localhost/Limestone_Tiles/products/Antalya_Blanc.html
The rewritten URI will be: /product_details.php?&furl=Limestone_Tiles
Whereas before, with the older rule, it would have been rewritten to: /product_details.php?&furl=Antalya_Blanc
If that's your intended behavior, then that's fine, except it's breaking the image rule:
RewriteRule ^(.*)/products/images/(.*)$ images/$1
The $1 backreferences the first grouped match (.*), the one before /products/images/, and not the actual image. It needs to be changed to:
RewriteRule ^(.*)/products/images/(.*)$ images/$2
You're probably better off changing the HTML content and fixing this image stuff there as opposed to creating a rewrite rule to try to correct it. You could try adding this to the header of your pages:
<base href="/">

Related

Rewriting old and new urls to same page with .htaccess

Have a page currently with the URL /results-details.php?mls_number=stringofnumbers.
I want it to re-write to: /results-details/stringofnumbers
However I want that to basically resolve back to the original page.
I'm also changing all the URLs on the site internally to point to the URL. So I have two re-write rules:
RewriteCond %{QUERY_STRING} mls_number=([0-9]+)$
RewriteRule (.*) /new-homes/results-details/%1? [R=301,C]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1
The second rule works fine on it's own with internal links in the /results-details/stringofnumbers form, but the first one doesn't chain properly to the second one and not sure what I'm doing wrong.
Basically trying to retain any links to the old URLs that might be out there but start using the new URS internally.
Suggestions?
YOu need to match against the actual request and not the URI, because the rewrite engine loops:
RewriteCond %{THE_REQUEST} \ /new-homes/results-details\.php?mls_number=([0-9]+)
RewriteRule ^ /new-homes/results-details/%1? [L,R=301]
RewriteRule ^results-details/([0-9]+)$ results-details.php?mls_number=$1

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.

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

htaccess url masking mod rewrite

I have done some URL masking and it all works very nicely. I have one issue that I am trying to resolve now:
RewriteRule ^(.*)/(.*)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3
with the above I can access the same page 2 ways, www.domain.com/premier-league/2010-2011/arsenal/ and www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal
is there a way in my rewrite rule I can redirect the URL (301 ideally) is someone does it through the untidy way "www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal"?
Thanks in advance
If you add another rewrite rule after this one that matches your 'inverse' pattern, and mark both as the [L]ast rule, that might work. I first rewrite the url to include the query string, and [C]hain that one to the next rule. After that we [R]edirect the browser.
RewriteRule ^(.)/(.)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3 [L]
RewriteRule ^(.+) $1%{QUERY_STRING} [C]
RewriteRule ^teams.php?competition=([a-zA-Z0-9]+)&season=([a-zA-Z0-9]+)&teamid=([a-zA-Z0-9]+)$ $1/$2/clubs/$3/ [R=301,L]
Note I haven't tested this or the regex of the second rule. Might need to adjust the character ranges a bit. Also I haven't tested the query string rewrite in the second rule.
Edit: See here for some common use cases of mod_rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.

Resources