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]
Related
I have a problem with mod_rewrite.
here is my .htaccess file:
#REWRITE
RewriteEngine On #Turn on the RewriteEngine
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^admin/(.*)/?$ admin.php?kappa=$1 [NC,L,QSA] # Handle Admin Panel
RewriteRule ^buypoint/([0-9]+)/?$ baltopoints.php?sid=$1 [NC,L] # Handle bit->Point requests
RewriteRule ^history/([0-9]+)?/?$ history.php?page=$1 [NC,L] # Handle Transaction requests
RewriteRule ^topupstatus/(.*)/?$ topupsta.php?ec=$1 [NC,L] # Handle index
RewriteRule ^refresh/(.*)?$ refresh.php [NC,L] # Handle Refresh requests
RewriteRule ^refill/(.*)?$ topup.php [NC,L] # Refill
RewriteRule ^topup/(.*)?$ topup.php [NC,L] # Refill
RewriteRule ^ucp/?(.*)?$ main.php [NC,L] # Handle index
RewriteRule ^logout/?(.*)?$ logout.php [NC,L] # Handle Logout
the rewrite only work for rule:
^buypoint/([0-9]+)/?$ baltopoints.php?sid=$1 [NC,L] rule
( buypoint/1 will rewrite to baltopoints.php?sid=1 )
otherwise, only work for first slashes (admin/viewbtx will rewrite to admin.php [with no query string])
Can someone help me about this problem?
The rule matches the regexp against the URi less the query string. A ? in the pattern makes the previous atom optional. This is std regexp syntax so ^a.php?e=$1$ will match a.phe=23 for example. You parse the query string using RewriteCond statement preceding the RewriteRule, for example:
RewriteCond %{QUERY_STRING} ^ec=(\d+)
and now %1 index number is available to the rules replacement string. Read up the examples in Apache Module mod_rewrite documentation.
I've figured it out by renaming admin.php to admincp.php and use RewriteRule ^admin/(.*)$ admincp.php?do=$1 [NC,L,QSA]rule instead of the one in question so i think it's apache bug or something
Same answer I gave to another question, it may be useful to you
How to htaccess redirect this long URL?
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 wanted to redirect/rewrite my name.php files to /name/
I found the solution on another topic (http://stackoverflow.com/questions/5527789/htaccess-rewrite-within-directory-hide-php-extension-and-force-trailing-slash)
Though, I wanted to learn it myself and started from scratch.
I first used this one, which makes eg .com/test/ show the content of .com/test.php:
RewriteEngine On
RewriteRule ^(.*)/$ $1.php
Then I tried the following, by itself, which redirects .com/test.php to .com/test/:
RewriteEngine On
RewriteRule ^(.*)\.php$ http://www.mydomain.info/$1/ [R=301]
So, both work on their own. But when I combine them, I get an loop error, even when I add [L] to it, which should mean the rules should only be used once. So this doesn't work:
RewriteEngine On
RewriteRule ^(.*)/$ $1.php [L]
RewriteRule ^(.*)\.php$ http://www.mydomain.info/$1/ [L,R=301]
I've probably made some stupid error but it seems logically to me...
Hope someone can point out my error.
Thanks.
Remove L-flag from first rule. That would stop "executing" and the second rule wouldn't be used. At the second rule you should keep the L flag, because it is the last one.
Because you have an external redirect with the R=301, adding L to it doesn't help as much as you need, as the redirect will come back to the server as a brand new request - where it again matches your first rewrite rule.
Instead, you need something like this:
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php\ HTTP/
RewriteRule ^ /%1/ [R=301]
RewriteRule ^(.*)/$ $1.php
Note that THE_REQUEST matches the entire line of the original request, e.g. GET /index.php HTTP/1.1. Even when %{REQUEST_URI} is rewritten to .php as part of the 2nd rule (where it will match on an internal sub-request), %{THE_REQUEST} is never rewritten, and this will ensure that the URL rewritten to .php doesn't match on the sub-request and result in another redirect sent back to the client.
Does anyone know what the problem is with this rewrite rule?
RewriteRule ^example/?(.*)/?(.*)/?(.*)$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
Shouldn't it work by detecting ? as a conditional?
Edit OK, let me rephrase my question.
How do you get Apache to ignore $1 - $3 if it's empty, and just instead go to example.php?
For example, instead of 3 lines:
RewriteRule ^example/(.*)/(.*)/(.*)$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
RewriteRule ^example/(.*)/(.*)$ example.php?id=$1&ud=$2 [QSA,L]
RewriteRule ^example/(.*)$ example.php?id=$1 [QSA,L]
I need one line to solve all 3 or more.
Greediness considerations make your rule equivalent to the following:
RewriteRule ^example/?(.*)$ example.php?id=$1 [QSA,L]
You probably want this instead:
RewriteRule ^example/([^/]+)/([^/]+)/([^/]+)/?$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
Based on the recent edit you did to your question, I'd use multiple rules:
RewriteRule ^example/([^/]+)/([^/]+)/([^/]+)/?$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
RewriteRule ^example/([^/]+)/([^/]+)/?$ example.php?id=$1&ud=$2 [QSA,L]
RewriteRule ^example/([^/]+)/?$ example.php?id=$1 [QSA,L]
Shouldn't it work by detecting ? as a conditional?
Sure, but backreferences are still filled in order. If the first parentheses (.*) match the empty string, then $1 is empty; there is no way to make this capture get "skipped".
The 404 error could be caused by a missing RewriteEngine. To solve the problem with the parameters, use:
RewriteEngine on
RewriteRule ^example/?([^/]+)?/?([^/]+)?/?([^/]+)?/?$ example.php?id=$1&ud=$2&ed=$3 [QSA,L]
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 /