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.
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]
I have the following Rewrite Rules set up:
RewriteEngine On
RewriteRule ^api/([A-Za-z0-9-]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php/other/$1 [NC,L]
Unfortunately these cause my server to throw a 500 error. Taken individually, they both work fine though.
My intention is that if the request is http://somesite.com/api/whatever/, the first rule will get triggered, redirecting to index.php/api/whatever/
If anything other than "api" gets sent as the second segment though, it will redirect to index.php/other/whatever.
Is my understanding flawed somehow? I thought that it would go down the list, and with the L flag, would stop executing once it hit something. Or is my syntax wrong?
Cheers
whenever you get a 500, check /var/log/httpd/error_log (or the equivalent path on your system.)
I'm pretty sure the hyphen char in your character group is a regex syntax error. (also, the [NC] flag makes [A-Za-z] redundant
Try:
RewriteRule ^api/([-A-Z0-9]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([-A-Z0-9]+)$ index.php/other/$1 [NC,L]
Or perhaps
RewriteRule ^api/([^/]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([^/]+)$ index.php/other/$1 [NC,L]
I think you need QSA flag, try like that :
RewriteRule ^api/(.*)$ index.php/api/$1 [QSA,NC,L]
RewriteRule ^(.*)$ index.php/other/$1 [QSA,NC,L]
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.
I'm developing a php application and I have a little issue with Apache and Mod Rewrite. Anyone knows what's wrong here?:
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^css\/css\.css css/css.php [L]
RewriteRule ^js\/js\.js js/js.php [L]
RewriteRule !^img\/.* index.php
When I put http://localhost/css/css.css appears index.php, maybe I'm missing something...
Why when the url matchs with the first rule apache doesn't stop the rewriting process?
'last|L' (last rule)
Stop the rewriting process here and
don't apply any more rewriting rules.
This corresponds to the Perl last
command or the break command from the
C language. Use this flag to prevent
the currently rewritten URL from being
rewritten further by following rules.
For example, use it to rewrite the
root-path URL ('/') to a real one,
e.g., '/e/www/'.
I have readed forums and docs since 3 hours and I still have the same problem.
Thanks in advance.
Centauro12, the problem is, that the [L] flag in fact stops propagation through the following rules, but then (if you are in an .htaccess file) the URL mapping starts over again. That means, all your rules will then be processed a second time. See the Apache Rewrite Guide for the details.
Therefore you need to explicitly disable rewriting for your rewritten php scripts:
RewriteRule ^css/css.php - [L]
RewriteRule ^js/js.php - [L]
or more compact (although perhaps not what you want):
# don't rewrite anything that really exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
I've found a solution:
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^css\/css\.css css/css.php [L]
RewriteRule ^css\/(.*)$ css/$1 [L]
RewriteRule ^js\/js\.js js/js.php [L]
RewriteRule ^js\/(.*)$ js/$1 [L]
RewriteRule ^img/(.*)$ img/$1 [L]
RewriteRule ^(.*)$ index.php?rewrite=$1
It works fine, but I don't know why it's necessary
RewriteRule ^css\/(.*)$ css/$1 [L]
and
RewriteRule ^js\/(.*)$ js/$1 [L]
I hope it hepls anyone.
Thanks! :)
try
RewriteRule ^/css/css\.css css/css.php [L]
RewriteRule ^/js/js\.js js/js.php [L]
RewriteRule ! /^img/.* index.php
ie. if you ^-anchor the pattern to the beginning of the string, start it with a /. patterns are matched against URL-paths, which start with /.
EDIT
above is valid for server config, virtual host, and directory context only. if the context is .htaccess, the per-directory prefix including the first slash is stripped before the rule is matched (and prepended afterwards), so no need for ^/ here.
You might have to set your rewrite base to "/" before starting your expression with "^css..."
RewriteBase /