redirecting a php url with 2 parameter - php

I am trying to set up my api to have 2 endpoints, and the simplest way I have seen to do this is to have a .htaccess file that would redirect something like www.website.com/api/category/platform to display the same as www.website.com/api/index.php?category=platform . The issue I am now running into is that I want it to include a second parameter such as www.website.com/api/index.php?category=platform&filter=price to be the same as www.website.com/api/category/platform/price or even www.website.com/api/platform/price
The current .htaccess file is
RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]

I figured it out and I was able to use something along the lines of
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ project3/api/index.php?category=$1&filter=$2 [L]

You can use smthing like this.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?category$ $1category/ [R=301,L]
RewriteRule ^ - [L]
RewriteRule . index.php [L]

Related

how to set right RewriteRule for epmty query

This is my .htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^(.*) data.php?chn=$1 [L,QSA]
I have 2 php files.
index.php and data.php
I to want call index.php when the request is for domain.com/ and data.php when the request is for domain.com/(anything) .
To reroute from index.php...
if (!empty($_GET['chn'])) {
require('data.php');
exit();
}
data.php will already have $_GET['chn'] available to it.
or if you aren't queuing off a get var then you can check the REQUEST_URI and do the same thing:
if (#$_SERVER['REQUEST_URI']!='/')) {
require('data.php');
exit();
}
RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/(\d+)*$ ./data.php?chn=$1
Try this.
Note: When using .htaccess in your desired case, placing / after the domain name will trigger the second RewriteRule case, meaning user will try to open literally domain.com/data.php?chn=.
You need to change your regex pattern from (.*) to (.+) to avoid rewriting your homepage otherwise the request for / will also be rewritten to data.php :
Try this rule :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/data\.php [NC]
RewriteRule ^(.+) data.php?chn=$1 [L,QSA]
its work ! thank you all using this code :
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)$ data.php?chn=$1 [L,QSA]

Rewrite - 2 files with same parameters and located in same folder

I know that was a frequent question here in SO, but I can't rewrite some dynamics URL.
Hope someone can help me.
I've 2 files located in the same folder, that receive same number of parameters:
.com/serie.php?id=__123__&nombre=__string__
.com/episodio.php?idSerie=__123__&query=__1x1__
My desired pretty url are this:
for serie.php: .com/__123__/string
for episodio.php: .com/__123__/string/1x1-some-other-strings-not-relevants-here
I was able to make the first rewrite, like this:
Options +FollowSymLinks
RewriteEngine on
# Rewrite Serie.php
RewriteRule ^([^/]*)/$ /serie.php?id=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /serie.php?id=$1&nombre=$2 [L]
Work fine, but the problem comes in the second rewrite.
The bold characters are "relevants" and I use this, to make some work.
I tried something like this:
RewriteRule ^([^/]*)-([^/]*)$ /episodio.php?idSerie=$1&query=$2 [L]
But I get 404 not found.
How can make this second rewrite?
Thanks in advice.
You can use:
Options +FollowSymLinks
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ serie.php?id=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ serie.php?id=$1&nombre=$2 [L,QSA]
RewriteRule ^([^/]+)/[^/]+/([^-]+)-[^/]*/?$ serie.php?id=$1&query=$2 [L,QSA]

htaccess With a different amount of variables

I have a website that on one specific page it requires an extra variable.
At the moment, the htaccess I am using is:
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
Which works fine with one variable.
How would I get it to work with :
http://tessite.com/index.php?page=test&id=1
So it looked like:
http://tessite.com/test/1
Thanks for any help or replies.
It can be coded like this:
RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&id=$2 [L]
RewriteEngine On
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&id=$2 [L]
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
Just add $2!
Also, [^/] isn't required, you can simply use ..

Issue with mod_rewrite when accessing directory

My code:
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
I am using WAMP and had setup a Virtual Host.
In my index.php, there is code to get page passed and checks if it exists(in database). If not, display an error message. It works fine.
Eg: http://mysite/contactus/
But it will not work if I use a a directory name as page_name in the URL. Eg: http://mysite/images/. This will display page not found error (ie. checks database and no page found, so display "not found"). But it will not display images,css(linked file) in the page. Also, it shows http://mysite/images/?page=images in addressbar.
Like that, if I goto js folder which is used to store javascript files, above problem occurs. So, problem is caused if any subdirectory's name is passed as pagename.
How to solve this ?
When http://mysite/images/ is supplied, mod_rewrite is redirecting to http://mysite/images/index.php?page=images instead of http://mysite/index.php?page=images
Edit
Please tell me how to block hotlinking of files and directory, and redirect back to index page or send some browser header error ?
I tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) http://%{HTTP_HOST} [R,L]
RewriteRule ^article/([a-z]+)/?$ /index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=$1 [L]
Edit
New code(semi-working):
RewriteEngine on
RewriteBase /
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1/ [R,L]
RewriteRule ^article/([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ http://%{HTTP_HOST}/index.php?page=$1 [L]
This code will clear the problem with not displaying pics and css when a directory name is mentioned. But whatever pagename i specify eg:http://mysite/contactus, it will goto URL: http://mysite/index.php?page=contactus. Even if I use a directory name eg: http://mysite/js, it will goto: http://mysite/index.php?page=js
I am very confused.
RewriteEngine on
RewriteBase /
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/*$ /index.php?page=$1 [L]
you have to put the slash in front.
Edit: changed the ? to *
My understanding is that your script is for documents only, not images or other resources.
Then you should ignore them right away. Try adding this line right after RewriteBase like this :
RewriteEngine on
RewriteBase /rewrite/
RewriteRule ^/(images|js)/(.*)$ - [L]
RewriteRule ^article/([a-z]+)/?$ index.php?page=article&name=$1 [L]
RewriteRule ^([a-z]+)/?$ index.php?page=$1 [L]
Then these subdirectories would be served right away, thus bypassing the next RewriteRule set.
For the problem with the directories I usually force a slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+[^/])$ $1/ [R]

.htaccess url rewriting

I need a rewrite rule to rewrite the following:
www.abc.com/page/xyz to www.abc.com/xyz
remove **"page"** from url
I think it's more like this:
RewriteBase /
RewriteRule ^(.*)$ page/$1 [L]
If I understand what you're trying to do.
Like so:
RewriteBase /
RewriteRule ^page(/.*)?$ $1 [L]
If you only want to redirect page/xyz then you can use:
RewriteEngine on
RewriteRule ^/page/xyz$ /xyz [L]
If you want to redirect anything within the page directory, then use:
RewriteEngine on
RewriteRule ^/page(/.*$)$ $1 [L]

Categories