URL Rewriting or .htacess not working - php

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]

Related

.htaccess rewrite $_GET variables

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]

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

rewrite URL using .htaccess only works on first query string

I'm trying to rewrite my url using htaccess but it only worked on the first query string
what I want is to rewrite this URL
acnologia.com/index.php?page=images&tag=nature
into something like this acnologia.com/images/nature
it does work on the first query string acnologia.com/images
but doesn't work if i add another directory after "images"
this is my .htaccess code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1&tag=$2
There's only one group in your original rule, so there's no $2.
Try this one:
RewriteRule ^([a-z\d_-]+)/([a-z\d_-]+)$ index.php?page=$1&tag=$2 [NC,L]
\d equals 0-9, and NC means nocase
The very basic rule will be like this
RewriteRule ^(.*)/(.*)$ index.php?page=$1&tag=$2
And more specific will be
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2
which #Andrew answered in comment.
Your final rewrite rule should be following. RewriteCond is specified so that js, css, png etc. files do not match this rewrite rule.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&tag=$2 [L,NC]
This tutorial is helpful.

Multiple MOD Rewrite Rules

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.

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?

Categories