Problem with RewriteRule - .htaccess

There is problem with it do not stop at first but continues to the third rules, the problem is just here.; There is several line rules and ending with rules that manage that does not fit on anything.
When I disable 3rd rules will /news not work but now, rules 1 works.
There are not any problems with the second rules.
I have gone through whether there is a loop through the query after news2.php?readmore=$1 and then on news2.php ask for news_cats.php, that is not the case.
I've also been looking at whether there is a loop in the htaccess file that is not the case.
What is problem?
RewriteRule ^news/([0-9]+)/[^/]*/?$ news2.php?readmore=$1 [L]
RewriteRule ^news_category/([0-9]+)/[^/]*/?$ news_cats.php?cat_id=$1 [L]
RewriteRule ^news news_cats.php [L]

Ooooh I've found the error. It is a loop in the htaccess.
3 rules news will fit not only news, but it would also capture the file "news"2.php
The solution.
RewriteRule ^news(?!2\.php) news_cats.php [L]

Related

mod_rewrite match from start of URI

Good afternoon,
I am having considerable difficulty getting my mod_rewrite rules to match from the start of the URI. I've looked through the manual but I must be missing some (probably obvious) syntax voodoo. Please consider the following:
RewriteCond %{REQUEST_URI} ^/(.*)/(foo|bar)/(.*)
RewriteRule ^(.*)/(.*)/(.*)/ destination.php?first=$1&second=$2&third=$3 [QSA,L]
If the 2nd URI parameter is foo or bar, (e.g. url.com/first/foo/third) this successfully redirects to destination, with the relevant parameters.
If foo or bar are present in the 3rd parameter (e.g. url.com/first/second/foo), I want a different redirect to occur. However, the following rule is ignored, with the above rule still taking priority
RewriteCond %{REQUEST_URI} ^/(.*)/(something-else)/(.*)
RewriteRule ^(.*)/(.*)/(.*)/ destination.php?first=$1&second=$2&third=$3 [QSA,L]
I think I must be missing an obvious way of forcing the rewrite to only match from the beginning of the URI- I've tried prepending slashes to try to force it to the root level, but without any joy so far.
Any help would be greatly appreciated!
Thanks
Edit
As it turns out, this was due to a basic RegEx mistake, rather than my mistakenly assuming the match needed to begin from the start of the URI. I won't edit the title as others may make a similar mistake.
.* is too greedy. Try these 2 rules:
RewriteRule ^([^/]+)/(foo|bar)/([^/]+)/?$ destination.php?first=$1&second=$2&third=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/(foo|bar)/?$ destination2.php?first=$1&second=$2&third=$3 [QSA,L]

php to html .htaccess Help

I have the following code in the my .htaccess file and the top Rewrite works fine the bottem one does not I know why but I dont kno how to fix it.
Its seeing RewriteRule ^([^/]*).html index.php?p=order&course_id=$1 [L] as the top rewrite command becuase of the hightlighed part and i dont want to put it in a dir
RewriteEngine on
RewriteRule ^([^/.]+).html
index.php?p=$1 [L]
index.php?p=about_us
RewriteRule ^([^/]+).html
index.php?p=order&course_id=$1 [L]
index.php?p=order&course_id=5
Thank you,
Can you give example urls that should match the pattern block and what you would like them to be rewritten to? That would be very helpful.
One thing I notice is that your first regexp you test if you match the pattern block with a + which means 1 or more times and the second one you check it with a * which means 0 or more so I don't think the second one will ever be called, although I am very new to regexps but it is just something I noticed.
These are very helpful resources for me:
http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
http://forum.modrewrite.com/
From the example of the urls you would be using, this should work:
# http://website.com/about_us/ rewrites to /index.php?p=about_us
RewriteRule ^([0-9a-z_-]+)/?$ index.php?p=$1 [NC,L]
# http://website.com/order/12/ rewrites to /index.php?p=order&course_id=12
RewriteRule ^order/([0-9]+)/?$ index.php?p=order&course_id=$1 [NC,L]
The second Rewrite might be:
# http://website.com/order/12/ rewrites to /index.php?p=order&course_id=12
RewriteRule ^([0-9a-z_-]+)/([0-9]+)/?$ index.php?p=$1&course_id=$2 [NC,L]
Depending on your page structure.

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.

How to have multiple rewriterules with .htaccess?

I am trying to build a multilingual website with Drupal.
I like to have the following url format
http://domain/[language]/[node id]
so I added the following rule to .htaccess for testing purpose
RewriteRule ^jpn/[0-9]$ jpn.html
The problem is that the rule is overwritten by the following rule
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
How do I have multiple rewrite rules?
Your second RewriteRule has the L Flag set, which means that if the rule matches, no further rules will be processed.
If you want your first rule to also stop any further processing, add the L Flag to it as well.
RewriteRule ^jpn/[0-9]$ jpn.html [L]
Also make sure that your second rule is listed last, because it matches everything (.*) and thus, Apache will never see any other rule after it.
Edited: the L Flag URL
I'm not sure this [L] really works in each case to avoid overwrite one each other. I'm not an expert on this, but i spent a day to figure out that you just need to add 1 Atome to a RewriteRule that did worked before without beeing overwriten, and it starts to get overwriten again from the first one. It's hard for me to believe that 2 different Rules, in 1 htaccess can properly work for 2 differents files.
Both Worked 100% well
RewriteRule ([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ display.php?$1&category=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ news.php?$1 [L]
displ
news
Link 2 displays the result of link1 instead..., just tell me why?
RewriteRule ([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ display.php?$1&category=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.htm$ news.php?$1&obj=$2&search=$3 [L]
displ
news

.htaccess mod_rewrite problem - shot myself in the foot?

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!

Resources