There are two kind of pages -
www.mysite.com/about.php
www.mysite.com/winners.php?y=2008
I want to make them like -
1. www.mysite.com/about
2. www.mysite.com/winners/2008
my htaccess is like -
RewriteEngine On
#removing .php ext(type-1)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#for type-2 (with parameter)
RewriteRule winners/([0-9]+)/?$ winners.php?y=$1 [L]
RewriteRule winning-photograph/([0-9]+)/?$ winning-photograph.php?y=$1 [L]
RewriteRule award-giving/([0-9]+)/?$ award-giving.php?y=$1 [L]
RewriteRule album-launch/([0-9]+)/?$ album-launch.php?y=$1 [L]
RewriteRule district-fest/([0-9]+)/?$ district-fest.php?y=$1 [L]
RewriteRule lifetime-achievement-awards/([0-9]+)/?$ lifetime-achievement-awards.php?y=$1 [L]
RewriteRule critics-award-for-tv/([0-9]+)/?$ critics-award-for-tv.php?y=$1 [L]
RewriteRule photography-book/([0-9]+)/?$ photography-book.php?y=$1 [L]
The two section of this htaccess are not working at the same time. what can I do? also - is there a way to simplify the 2nd section?
Your first RewriteRule is basically preventing all the other rules from executing. It does that rule for both types of your URLs, then the [L] flag stops it from evaluating the other rules.
I would say put that rule at the very end. Do all the more-specific ones first, then have that as a generic one for all other cases, only to be evaluated if the rest of the rules didn't match the current URL.
Related
www.example.com/category/sub_category/Having problem with the rewririte rules in .htaccess file.
my current .htaccess file looks like this.
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
RewriteRule ^([0-9]+)$ gallery.php?id=$1 [NC,L]
I'm trying create urls like the following.
www.example.com/product_name/1
www.example.com/category/sub_category/21
www.example.com/category/sub_category/product_name/233
www.example.com/gallery/872
www.example.com/gallery/872 is redirecting to www.example.com/category/sub_category/872 instead of gallery.php?id=872
edit:corrected url from www.example.com/gallery/872 to www.example.com/category/sub_category/872.
Your issue is that the first rule matches, the last one can never get applied...
RewriteEngine on
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [NC,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [NC,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [NC,L]
Rule of thumb: first the specific exceptions, then the more general rules.
The NC flag does not make sense if you also specify both, lower and upper case letters in your regex patterns. It is either/or, not and.
(note: I also included the correction #anubhava posted in his answer)
Your last rule will need regex modification since you're matching /gallery/872 and your pattern is only matching 1 or more digits.
RewriteRule ^gallery/([0-9]+)/?$ gallery.php?id=$1 [NC,L,QSA]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ products.php?cat=$1&id=$2 [QSA,L]
RewriteRule ^([^/]*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_categories.php?cat=$2&id=$3 [QSA,L]
RewriteRule ^(.*)/(.*)/([0-9a-zA-Z_-]+)/([0-9]+)$ product_details.php?cat=$3&id=$4 [QSA,L]
Also you need to recorder your rules like I showed above.
I have the following page:
http://localhost/?news=19&page=2
I want to rewrite this so that it goes kind of as follows
http://localhost/news/19/page/2
This is my .htaccess file, but my code dont work :(
RewriteEngine ON
RewriteBase /
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(.*)$ /?news=$1 [L]
RewriteRule ^news/(.*)/page/(.*)$ /?news=$1&page=$2 [L]
You should specify more precicely what the allowed type of character is and you should make sure your previous rules don't cause your later rules to be ignored.
So you should put your most specific rule first and if you want digits for your news- and page ID's, you should use for example:
RewriteRule ^page/(.*)$ /?page=$1 [L]
RewriteRule ^news/(\d*)/page/(\d*)$ /?news=$1&page=$2 [L]
^^ just a digit instead of any character `.`
RewriteRule ^news/(.*)$ /?news=$1 [L]
Hello this is my htaccess:
RewriteRule ^[a-zA-Z]*$ /index.php?a=profile&u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/home$ /index.php?u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/blog$ /blog.php?u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/contact$ /contact.php?u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/portfolio$ /portfolio.php?u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/page_([a-zA-Z0-9_-]+)$ /page.php?id=$2&u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/post_([a-zA-Z0-9_-]+)$ /post.php?id=$2&u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/blog_([a-zA-Z0-9_-]+)$ /blog.php?id=$2&u=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/project_([a-zA-Z0-9_-]+)$ /project.php?id=$2&u=$1 [L]
all the lines work besides the first one, I think I might need to use conditions?
here is what im trying to do if someone goes to site.com/john for example they will be sent to /index.php?a=profile&u=$1 which is the profile for the username=john in this case. but what happens is it sends me back to index.php
i think you should remove [L]
RewriteRule ^([a-zA-Z]*)$ /index.php?a=profile&u=$1
The [L] flag causes mod_rewrite to stop processing the rule set. In
most contexts, this means that if the rule matches, no further rules
will be processed. This corresponds to the last command in Perl, or
the break command in C. Use this flag to indicate that the current
rule should be applied immediately without considering further rules.
I am having issue passing get variables.
index?p=calendar refers to calendar.php located in pages/calendar.php and index.php is in root.
my URL is localhost/researchportal/calendar/11/2011
Calendar has 2 get variables, month and year. i.e calendar.php?month=11&year=11
here is my rule, but its not working.
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]
I also tried
RewriteRule ^calendar/([0-9]+)$/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
.htaccess file
RewriteEngine On
RewriteBase /researchportal/
RewriteRule ^/calendar/([0-9]+)$ index.php?p=calendar [QSA,L]
RewriteRule ^users/login /researchportal/pages/login.php [L]
RewriteRule ^users/logout /researchportal/pages/logout.php [L]
RewriteRule ^users/register logout.php [L]
RewriteRule ^profile/([0-9]+)$ index.php?p=profile&usr_id=$1 [QSA,L]
RewriteRule ^profile/edit/([0-9]+)$ index.php?p=edit&usr_id=$1 [L]
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?p=$1 [L]
^calendar/([0-9]+)$/([0-9]+)$
Why are you ending ( $ ) it twice?
^ starts and expression and $ ends it. There is no reason to have neither in the middle of an expression.
RewriteRule ^calendar/([0-9]+)/([0-9]+)$ pages/calendar.php?month=$1&year=$2 [L]
Should work better. If you for some reason would also want some query variables in the url, you can replace [L] with [L,QSA] aswell.
I think the problem is because you have an additional $ character.
RewriteRule ^calendar/([0-9]+) *$* /([0-9]+)$ index.php?p=calendar&month=$1&year=$2 [L]
I'm trying to return a success notice if the $_GET['success'] is set. It works fine if I input it like this: mail?inbox&success
But I'm trying to make it work if I input it like this: mail/inbox/success or mail/inbox&success
I'm not very experienced with mod_rewrite and I havent found a solution yet so I'm asking here. How would I get it to work with the examples above? This is what the .htaccess looks like right now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^user/([^/]*)$ /userinfo?name=$1 [L]
RewriteRule ^items/([^/]*)$ /items?weapon=$1 [L]
RewriteRule ^mail/inbox /mail?inbox [L]
RewriteRule mail/(.*)/(.*)$ /mail?read=$2
You could change
RewriteRule ^mail/inbox/ /mail?inbox [L]
to
RewriteRule ^mail/inbox/(.*) /mail?inbox&$1 [L]
Depends on what other combinations you want to rewrite.
Adding the line
RewriteRule ^mail/inbox/success /mail?inbox&success [L]
before the first rule should do it also, but only for this one case and may influence other rules.