php to html .htaccess Help - .htaccess

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.

Related

Modrewrite for pretty url

How can modrewrite be done in this above situation:
Lets say the website us is: www.real-estate.com
Then we have the first mod rewrite:
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So this will rewrite to something similar: www.real-estate.com/florida and it will list all the real estates in florida.
Then we add this rule:
RewriteRule ^([a-zA-Z\-]+)/(.*)+$ details.php?project=$2
This will rewrite to www.real-estates/florida/project-one and it will display details from that project.
But if I access the link for the city like this: www.real-estae.com/florida/ (with slash last) it will jump me to the second mod rewrite rule, taking me to details.php with an empty variable.
What is the correct solution to slove this problem, so that if the users adds a slash after the city it will still display the projects in that city and not the details page with empty var ?
After playing around I found that his works, but I do not know if it is the correct solution:
I replaced this:
RewriteRule ^([a-zA-Z\-]+)/+(.*)$ details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
With this:
RewriteRule ^([a-zA-Z\-]+)/([a-zA-Z\-]+) details.php?project=$2
RewriteRule ^([a-zA-Z\-]+) cities.php?city_url=$1
So I have replaced (.*)$ Why does this work and the other way not I do not know.. Maybe someone can explain.
Probably it fixed the immediate problem but your rules might still match more than required due to faulty regex used and not using anchor $.
For your case use these rules:
RewriteEngine On
RewriteBase /
RewriteRule ^[a-zA-Z-]+/([a-zA-Z-]+)/?$ details.php?project=$1 [L,QSA]
RewriteRule ^([a-zA-Z-]+)/? cities.php?city_url=$1 [L,QSA]
Apache mod_rewrite Introduction

Two rules in one .htaccess file not working

Below is my code for .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /products/product-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /buy/buy-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2
First rule is working fine but its not taking the second rule...Unable to understand what is happening here...
Original URL's are like this
www.example.com/products/product-full-view.php?src=somevalue&id=somevalue
and for second one
www.example.com/buy/buy-full-view.php?src=somevalue&id=somevalue
Please help me to run second rule also.
Thanks in advance
You're trying to match the same pattern, give or take an optional trailing slash, four times. By the time you reach the last two rules, your URL is already rewritten to something else.
You probably want something that looks more like:
RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ /products/product-full-view.php?id=$1 [L]
RewriteRule ^buy/([0-9]+)/?$ /buy/buy-full-view.php?id=$1 [L]
Or:
RewriteEngine On
RewriteRule ^products/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2 [L]
RewriteRule ^buy/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2 [L]
Or something to that order anyway.
Note the stub and the [L] each time: the products/ and buy/ avoid that the same URL refers to two different locations, and the [L] (for "Last") tells the rewrite engine to stop processing rules when it gets matched.
If a URL matches the second rule, it also matches the first rule. After applying the first rule, the resulting URL no longer matches the second rule. That's why the second rule is never applied.

mod rewrite exclude all but php

Is it possible to edit htacces in such a way that only the following url is rewritten and the rest isn't?
http://www.example.com/index.php?x=foobar
to
http://www.example.com/foobar/
I want the pages not having x=... as a variable to behave normally
I got the following but that doesn't work
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/ index.php?x=$1
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav|txt)$
Who can help me?
First off, the RewriteCond must be put before the RewriteRule to which it belongs.
But I think that you need another approach for your case, something like this:
RewriteRule (.*)\.php - [PT,L]
RewriteRule ^(.*)/$ index.php?x=$1
The first rule Passes Through (PT) every PHP page, so the second rule is only applied to all non-PHP requests.
That second rule only applies to a "simple path", no matter if this path has a dot in it or not (e.g. hello.gif/ will match, too).
If this does not work for you, then you might consider one of these points to start further research:
the pattern ([^\.]*) matches everything that does not have a dot in it
see RewriteCond to skip rule if file or directory exists for RewriteConds where the following RewriteRule is only used if the request does not point to an existing file or directory
Hope this helps.

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})/(^/]+)/?$

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

Resources