.htaccess mod_rewrite problem - shot myself in the foot? - .htaccess

I have a page called category.php5 that uses $_GET["category"] to fetch the right content from the database. I want to pretty it up so is looks like:
sinaesthesia.co.uk/category/psoriasis
which would equal:
sinaesthesia.co.uk/category.php5?category=psoriasis
I have successfully done this sort of rewriting before, but since I can't get it to work now, I'm worred that I might have rules in place that are somehow screwing me. Here is my entire .htaccess file - the last couple of lines are supposed to do the above rewrite:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
#Translate default page to root
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]
#Translate /category/psoriasis to /category.php5?category=$1
RewriteRule ^category/(.*) /category.php5?category=$1 [L]
When I manually enter category.php5/category=psoriasis, it works great. When I enter category.php5/category/psoriasis, it doesn't. I'm worried that my line that changes a html/ to html? is an error, however when I take that line out, it still doesn't work. Everything else works as expected.

As a general strategy, strip down your file by commenting everything out, then re-enable things piece by piece until you find the rule that causes it to break.
Bear in mind that browsers sometimes cache redirects, so starting a fresh browser instance is a good idea. A useful service is http://web-sniffer.net/ which will give you an uncached result.
In general, looking at your set of redirects, this seems a little convoluted to me because of the chaining/sieve -type system you seem to be using. Instead, I would recommend starting with URLs that can be identified specifically, e.g. starting with
RewriteRule ^category/(.*) /category.php5?category=$1 [L]
and then leaving the rather messy .html => .php conversion stuff towards the end, if you end up needing it at all. I've done a lot of sites using redirects and have never needed generic conversions like that, so they should be avoidable.
Also bear in mind that .* means matching anything or nothing, so you probably want to use .+ instead.

Ah: because I have a document called category.php5 and I'm trying to use category/psoriasis, the server tries to resolve that as category.php5/psoriasis, which fails. Fixed it now!

Related

.htaccess RewriteRule query isn't working

I can't get the following RewriteRule to work.
I have a PHP SQL query to display a web page. It requires a RewriteRule rule which I'm trying to achieve in a .htaccess file.
Here is the full URL at the moment.
www.example.com/category/sub-cat/page.php?art_url=a-page-of-mine
I can't get it to do
www.example.com/category/sub-cat/a-page-of-mine
My Code below:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 /error-404.php
#error 404
RewriteRule ^error/?$ error-404.php [NC,L]
RewriteRule ^category/sub-cat/(0-9a-zA-Z]+) category/sub-cat/page.php?art_url=$1 [NC,L]
Can someone help me out?
AS I said in the comments
Missing a few things here (0-9a-zA-Z]+) like [- as in ([-0-9a-zA-Z]+)
This is going to bite you too...
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Match everything that doesn't have a dot and add .php to it with the [L] last flag. I would bet it will never pass that one in the first place.
Generally you want the more specific rules first, followed by the more generic ones last.
Also if I recall correctly the NC i no case, so you can get rid of the A-Z and just do [-a-z0-9]+
A better way
I try to avoid query string rewrites and rely on the URI method of rewriting common in MVC frameworks
example.com/index.php/category/sub-cat/a-page-of-mine
And then use a router and HTACCESS to only remove the index.php it's much simpler that way.
I have a pretty bare bones router on my GitHub page that shows how to route URL's like that.
https://github.com/ArtisticPhoenix/MISC/tree/master/Router
One big issue with messing with the query string is you can lose the ability to use $_GET the way it's designed to be used for things like search forms etc. So it's better to route not rewrite. Also the MVC way gives you a single entry point for all requests to go through which can make it easier to manage things like Constants, and Autoloaders....
Oh well, this is broken of course:
(0-9a-zA-Z]+)
The charclass lacks the opening [ and doesn't contain/match a literal - as well.
Right. To get this working I needed to add QSA as in [QSA,NC,L]. After how many weeks!!??

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.

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

RewriteRule does not work, while the rest do

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.
http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/
all are actually using http://kn3rdmeister.com/blog.php.
The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.
the .htaccess:
RewriteEngine On
RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]
RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]
The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.
The script works, but the redirect doesn't. Going directly to
http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
will load the right content. However, going to
http://kn3rdmeister.com/2012/07/04/amsterdam-ave/
doesn't.
Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L]
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]
But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.
Whilst webarto comments are good advice, your problem is a missing [:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$
not
^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$

htaccess - Rewrite rule and/or condition to fix totally different discontinued or incorrect incoming link

Well, I am stuck again. Two days of reading and again, found some close solutions but, nothing fits and all my experiments failed.
This is a continuation of my question:
here at stackoverflow
The 4 rules below take my incoming links:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and beatifies it.
Also allows users to use the beatified version right in address bar:
http://somedomain.com/1234567
Here are my working rules:
RewriteRule ^([\s]*)$ /getme.pl [L] ## in case there is a space or nothing.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Works great but, I discovered there are some old links to the site out there:
http://somedomain.com/oldversion.php?id=123456789
And
http://somedomain.com/oldversion.php?r=86this&id=123456789
I would like to just grab the id=[0-9] and integrate it with my working rules.
I suppose, the rule would be inserted between the second and third rules above.
I tried various attempts (about 100!) like:
RewriteRule ^(oldversion\.php)?([a-z]{1})=([a-z0-9]*)&([a-z]{2})=([0-9]*)$ /$4? [L]
RewriteRule ^(oldversion\.php)?([a-z]{2})=([0-9]*)$ /$3? [L]
As you see, two days of reading and nothing is sinking in for me.
I tried several variations of the working rules I already have as well, to no avail.
Can't I just get the 123456789 off of the outdated .php urls somehow and stick it in my existing rules?
Thanks for your help and explaining down to my level co, I just might be able to understand...
Put this at the end of your .htaccess file:
RewriteCond %{QUERY_STRING} id=([^&]+)(&|$) [NC]
RewriteRule ^oldversion\.php$ /%1? [L,R=301,NC,NE]
For a URI of /oldversion.php?r=86this&id=123456789 it will internally redirect to /123456789
Remember RewriteRule just matches your URI and it cannot match your QUERY_STRING.

Resources