rewrite URL using .htaccess only works on first query string - php

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.

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]

Cleanning urls with htaccess

i am trying to use RewriteRules to get clean urls using my HTACCESS file.
here is what i have so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The above code takes a url that looks like this company.com/about.php and turns it into company.com/about/ so all my links url are like this "/about/" i didnt add the .php because of the rewrite rule.
what i am trying to do now is add a rule that will clean my url when parameter is passed. for example
company.com/about/?profile=member_name i want it to look like company.com/about/member_name
i have tried the two rewrite code below but it doesn't work.
RewriteRule ^([a-zA-Z0-9]+)$ /our_work.php/ [L]
RewriteRule ^/our_work.php([^/\.]+)/?$ ?project=$1 [L]
please keep in mind that my file extension is already being striped from the url.
Please help
Thank you in advance
Apache provides documentation on the use of RewriteRule that you should read before proceeding.
In this particular case, you can rewrite /about/{member_name} to /about.php?profile={member_name} by implementing this rule:
RewriteRule ^about/([a-zA-Z0-9]+)(/)?$ about.php?profile=$1 [L]
The rule states: The requested URL must begin with about/ and be followed by letters and numbers (match #1) and may be proceeded by an additional ending forward slash (match #2); and it will be substitued with a URL about.php with profile query string's value as match #1. The L flag indicates that any rules that follow this rule should be ignored.
Attempting to "cascade" rules will not work with your current set of rules because the /about/{...} is not matched by the first rule.

my .htaccess file rewrite something wrong

I have a problem with .htacces file.
Can you tell me please what am I doing wrong here?
I will show you some screenshot: This image shows my Cpanel my Script files is in home/lulu/public_html/
And another problem is: When i click my friends picture his/her profile it shows me my profile but url is www.ecoshoptr.com/rajesh
last proflem is when i click my friends button page said (Not: I have no friends folder )
<li>Friends</li>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^ecoshoptr.com$
RewriteCond %{REQUEST_URI} !^/
RewriteRule (.*) http://www.ecoshoptr.com/$1
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1
Change the following two lines:
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=$1
to
RewriteRule ^(.*)/?$ index.php?id=$1
Your rules would only rewrite urls that has characters a-z in lower and upper case, an underscore, a - and digits 0-9. The url friends/Name wouldn't match that rule because it has a / in it. The rule I have will accept that. Also I combined the two rules into the by adding a ? after the / because it will say that it might be there and it might not be there.
I dont if you left it out but the [L] param prevents it from further rewriting and thus not ending up in your last rules
Maybe you can turn on your RewriteLog if that is available on your apache version
RewriteRule foo bar [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

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]

Categories