Multiple MOD Rewrite Rules - php

What I'm trying to do
I would like to redirect:
www.example.com/home/place to www.example.com/index.php?location=place
www.example.com/home to www.example.com/index.php.
What I've done
In .htaccess, I have the following line:
RewriteRule ^home/([^/]*)$ index.php?location=$1 [L]
This does part 1. However, with this in place, part 2 does not work. However, if I write this before the above:
RewriteRule ^home index.php [L]
then part 2 works, but part 1 doesn't.
Long story short, I cannot work out how to enable both rules. Is this possible, and if so how?

The problem is that
RewriteRule ^home index.php [L]
will match both examples as they both begin with "home". Try the following:
RewriteRule ^home/?$ index.php [L]
This will match /home and /home/ but not /home/foo.

Your first rule has the parameter as "optional" because you're using a * instead of a +:
RewriteRule ^home/([^/]+)$ index.php?location=$1 [L]
Additionally, you don't have a $ in your second rule to indicate the end of the URI:
RewriteRule ^home/?$ index.php [L]
Now the rules can be in any order you want.

Related

Changing URLs when using $_get to determine webpage

I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]

htaccess rewrite index.php to .html

I have following rewriterules:
RewriteEngine On
RewriteRule ^([^\-]*)-(.*)-von-(.*)\.html$ $1index.php?filter=$2&marke=$3 [QSA]
RewriteRule ^([^\-]*)-von-(.*)\.html$ $1index.php?marke=$2 [QSA]
RewriteRule ^([^\-]*)-(.*)\.html$ $1index.php?filter=$2 [QSA]
RewriteRule ^(.*)\.html$ $1index.php
The first 3 rules are working, but the fourth, just rewrite index.php to .html is not. What is wrong here?
EDIT:
The URL is example.com/folder/subfolder/index.php
In the folder I got following htaccess:
RewriteEngine on
RewriteRule ^subfolder(.*) subfolder/$1
And the htaccess in subfolder is the one above
Now the URL for the first rule is example.com/folder/subfolder-value1-von-value2.html and works, for the second and third rule it's example.com/folder/subfolder-value1.html and example.com/folder/subfolder-von-value2.html
So with logic the fourth rule should also work just without the parameters but it's not working
Place this in /folder/.htaccess:
RewriteEngine on
RewriteRule ^subfolder\.html$ subfolder/index.php [L,NC]
RewriteRule ^subfolder(.+) subfolder/$1 [L,NC]
You shouldn't add $1 to the second part of the rule.it'll append the first matching group to the index.php file name. what you are currently getting is 'indexindex.php'
if you just want to rewrite index.html to index.php then you can place following line at the end of the file.
RewriteRule ^index.html$ index.php [L,NC]
also you might wanna remove $1 part from other lines as well.
Can you try this & see ?
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php

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]

mod_rewrite not passing query string

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?

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