mod_rewrite not passing query string - php

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?

Related

Redirect with htaccess from index.php to another.php file

I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".

.htaccess rewriteRule conflict

I am writing a code and build an htaccess file which has the following contents:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?id=$1 [NC,L] # Handle page requests
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?cat=$1 [NC,L] # Handle category requests
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?auth=$1 [NC,L] # Handle author requests
The first rule works fine but the latter seems having conflict with the first rule. Everytime I access a url at http://example.com/category/foobar or http://example.com/author/fooauthor/ I always get a message that the page cant be found because it is still trying to open the index file.
Is there any possibility to get around with this?
You can set general rule in the last line:
RewriteEngine on
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?auth=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?id=$1 [NC,L]
This issue happened because first line support all urls

.htacces url_rewrite difficulties

I have a problem with the configuration of the .htaccess of small website that I'm working on.
I want all pages to be redirected to index.php?page=REQUEST and that file will find in the database the content for the requested page.
The problem occurs when I have installed a forum, so I want these forum pages to redirect to the index.php?page=forum&params
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} /(.*).html
RewriteRule ^(.*)forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L]
RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]
Evetything works fine, except the forum part. How do I need to change the .htacces?
RewriteEngine on
RewriteRule \.(jpg|png|gif|svg|css|js)$ - [L]
RewriteRule ^(.*)/forum/topic/(.*)?$ index\.php?page=forum&lang=$1&topic=$2 [L]
RewriteRule ^(.*)/forum/category/(.*)?$ index\.php?page=forum&lang=$1&category=$2 [L]
RewriteRule ^(.*)/(.*)(\.html?)$ index\.php?lang=$1&page=$2 [L]
RewriteRule ^(.*)(\.html?)$ index\.php?page=$1 [L]
The problem appears to be that your RewriteCond is matching requests that end in .html. As your forum URLs don't end in .html the condition for the subsequent RewriteRule is never met.
There are some other possible problems too:
^(.*)forum will match www.url.com/en/ when it looks like you probably just want en
category/(.*) will match any characters, including forward slashes and the like. Presumably you just want it to match a decimal identifier.
Links to things that aren't covered by your rewrite config e.g. images
I'd probably rewrite your config to look something like this (N.B. not tested in Apache; only in a regex debugger):
RewriteEngine on
# only match forum URLs
# e.g url.com/en/forum/category/12345
RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+
RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L]
# match all URLs ending in .html
# e.g. url.com/en/foo.html
# and url.com/foo.html
RewriteCond %{REQUEST_URI} ^/.+\.html$
# a bit complicated, this matches both
# /apage.html
# /folder/apage.html
RewriteRule ^(?:/(.+))?/(.+)\.html$ index.php?lang=$1&page=$2 [L]
The second RewriteRule should always provide a value for page but only provide a value for lang if the URL is of the form /lang/page.html. This should be OK if your index.php file can accept an empty lang parameter or supply a default value.
Alternatively, if you don't mind keeping your existing regex and it's only images, CSS etc you want to bypass in URL rewriting you can add some rules at the start to skip them e.g.
RewriteEngine on
# don't actually rewrite, and stop processing rules
RewriteRule \.(jpg|png|css|js)$ - [L]
# only match forum URLs
# e.g url.com/en/forum/category/12345
RewriteCond %{REQUEST_URI} ^/.+/forum/category/[0-9]+
RewriteRule ^/(.+)/forum/category/([0-9]+) index.php?page=forum&lang=$1&category=$2 [L]
etc...

URL Rewriting or .htacess not working

I am trying to rewrite the url like
http://test.com/1234
to
http://test.com/index.php?a=1234
and my .htaccess file
RewriteEngine On
RewriteRule /^[a-zA-Z0-9]/?$ index.php?key=$1 [NC,L]
but nothing is happening it simply shows object not found error, .htaccess file is already in the root directory so help needed
P.S. I am a beginner in rewriting.
Try this rewrite rule:
RewriteEngine On
Options +FollowSymlinks
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?key=$1 [NC,L]
You need the brackets to group characters and re-use them as the $1 parameter.
You don't need the /
If you have a pattern like [0-9] you need a * or + if you want to match more than one character.
Btw, if the key parameter only accepts numbers, use:
RewriteEngine On
Options +FollowSymlinks
RewriteRule ^([0-9]+)/?$ index.php?key=$1 [NC,L]
try this
RewriteRule ^(.*)$ index.php?a=$1 [L,QSA]
^(.*)$ results in passing down the whole request path as one parameter
QSA results in appending any query string to the request
?a=$1 specify how the parameter is passed down
Try this rewrite rule:
RewriteRule ^(.*)$ index.php?a=$1 [QSA,L]

Apache Mod Rewrite: RewriteRule with L argument. What's wrong?

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 /

Categories