I want to achieve the following
Any links like mydomain.com/anyword should be rewritten to index.php?pg=$1
example:
mydomain.com/new -->index.php?pg=new
mydomain.com/purchased -->index.php?pg=purchased
mydomain.com/login -->index.php?pg=login
Any links like mydomain.com/anyword/anynumber should be rewritten to index.php?pg=$1&id=$2
example:
mydomain.com/new/12 -->index.php?pg=new&id=12
mydomain.com/purchased/240 -->index.php?pg=purchased&id=240
mydomain.com/login/10 -->index.php?pg=login&id=10
I have used the following htaccess code.
The first one works properly.
Regarding the second one, the entire parameter is passed to pg and there is no parameter id at all.
That is in case of url
mydomain.com/purchased/240
in my php $_Get has only 'pg' parameter and 'id' parameter at all
and pg=/purchased/240
Please suggest the proper way to do it.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
RewriteRule ^([^/]+)/(\d+)/.*$ /index.php?pg=$1&id=$2 [NC,QSA]
Thanks
EDIT: Correct solution in case anyone made need it
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+) /index.php?pg=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
This is because of the catch-all pattern. (.*) matches everything and rewrites it to the target. For your example, your first rule matches both requests, /foobar/ and /foobar/123 are rewritten to /index.php?pg=request . To fix this, you need to fix the order of your rules, you need to put your specific rule before the catch-all rule :
RewriteRule ^([^/]+)/(\d+)/?$ /index.php?pg=$1&id=$2 [L]
RewriteRule ((?!index.php).+) /index.php?pg=$1 [L]
Related
I wanna to get rid of GET string parameters, from keys.
For example this is initial uri:
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25&user_city=ohio
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25
http://example.com/api/get_users_method?user_city=ohio
I wanna make like this:
http://example.com/api/get_users_method/18/25/ohio
http://example.com/api/get_users_method/18/25
http://example.com/api/get_users_method/ohio
How can i get this result? I found some solution, to edit htaccess file, but it doesn't work unfortunately, maybe i made some mistakes in htacces file? I'm using php. Thanks!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by that rule.
You will 3 separate rules in your root .htaccess (a level above api directory)
Options -MultiViews
RewriteEngine On
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3&user_city=$4 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/?$ $1?user_city=$2 [NC,L,QSA]
I'm trying to create shorter urls to some of my pages.
Previously I had a system that URLs were like /index.php?m=page&title=Page-Title
The piece of code I have now allows me to remove the everything 'm=' part. /index.php?m=page in here, the 'page' is a name of different modules, so this part might still change. I want to change the url only from 'page'.
How would I rewrite this so that my URL would be composed only as such:
/page=Page-Title
Would that be even possible since I'm using $_GET in 2 places?
My current .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L]
You can have your rules like this:
RewriteEngine On
RewriteRule ^page=([^/]+)/?$ /index.php?p=page&t=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/?p=$1 [L,QSA]
Hoping someone can help me here. In my .htaccess file, I've got a rewrite rule written as:
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
for a URL that would be something like
localhost/wrestling.php?id=something
And now what I'm trying to do is change a URL like
localhost/category.php?id=something
to
localhost/id/category
The thing to note with this scenario is that this time I'm trying to use the id part as the thing after the first "/" and then add the category part after the second "/".
Below is what my entire .htacces file looks like at the moment.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]
# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Any thoughts guys?
You can try
RewriteRule ^([^/]*)/category/?$ /category.php?id=$1 [L]
Try this :
RewriteRule ^([^\/]*)/([^\/]*)$ /$2.php?id=$1 [NC,QSA]
You need two captured parameters.
This expression was tested with http://htaccess.madewithlove.be/.
I have the following rewrite rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ci.php?/$1 [L]
The above was wiping out the GET parameters. After reading some posts on here, I used the [QSA] parameter with hopes that the GET variables will be appended to the url. But it isn't working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ci.php?/$1 [L,QSA]
I am using codeigniter and I am not sure why my rule has to be ci.php?/$1 (meaning, why its not ci.php?page=$1 or something like that). But that is the only thing that works. And I suspect that appending the GET variables to such a URL is not working out.
How do I get out of this?
What fixed this was swapping the QSA and L in the last line.
RewriteRule ^(.*)$ ci.php?/$1 [QSA,L]
I want to redirect from /gallery/X to gallery.php?category=X
But when I actually goto the address, my old $_GET variable 'category' is transformed into the form:
$_GET['category'] = "X.php/X"
This is what my .htaccess looks like:
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/(.*)$ /gallery.php?category=$1 [L]
I am very bad at RegEx and no almost nothing about .htaccess. I have been trying to play around with this rewrite rule to preserve the $_GET variables, but nothing I do seem to work. What am I supposed to do here?
Thanks!
Move your rules around:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/(.*)$ /gallery.php?category=$1 [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Also, add QSA to your action, [L,QSA]. This appends the query string during the redirect, so it should keep your previous data.
Explanation:
Your rewrites need to be in a specific order because the [L] option is like making a completely new request to the server. So, when your request for gallery/X came in, it rewrote your request to /gallery.php?category=X. When this page was requested by the server, it matched your first rule, which means it was being seen as X.php/X which was then being returned to the original request as the extra $1.
Sounds confusing but I think that's what was going on.