url rewrite not reading in php - php

I am beginner in url rewriting and i have studied about it from various sources.Everything seems to be working till now but this is just not working.
I have this line of code in my htaccess file.
RewriteRule ^notice/all/page/([0-9]+)/?$ files/all_notice.php?show=all&page=$1 [NC,L]
This is the url i am visiting
http://localhost/notice/all/page/1
and when reading variables in php like this-
<?php
$type=$_GET['show'];
$page=$_GET['page'];
echo $type.'--'.$page;
?>
This is what i get
all--notice
I am getting the show variable as it is but instead of page number i am getting notice.
What is going wrong?

Your RewriteRule is this:
RewriteRule ^notice/all/page/([0-9]+)/?$ files/all_notice.php?show=all&page=$1 [NC,L]
Try this:
RewriteRule ^notice/all/page/([0-9]+)/?$ /files/all_notice.php?show=all&page=$1 [L]
The only difference is the addition of a / in front o files/… and removing the NC so it is only [L].
EDIT: And perhaps you should see what happens if you change that $1 to $2, $3 or even $4 to see if any of those values get passed through.

Try this:
RewriteRule ^notice/all/page/([0-9]+)?$/ files/all_notice.php?show=all&page=$1 [NC,L] //for urls like localhost/notice/all/page/1/
RewriteRule ^notice/all/page/([0-9]+)?$ files/all_notice.php?show=all&page=$1 [NC,L] //for urls like localhost/notice/all/page/1
You have ?$ after the / but it should be after the regex class

Related

How to set .htaccess for 2 type get value on the same page?

For this page test.php
i have to use 2 format url
www.example.com/test/1234
and
www.example.com/test/1234/abcd
My .htaccess is
RewriteRule ^test/([^-]*)/([^-]*)$ /test.php?one=$1&two=$2 [L]
It's work good with www.example.com/test/1234/abcd but redirect internal error with www.example.com/test/1234
I want to know how can i edit .htaccess for work good with my 2 url format on test.php ?
Try the following instead:
RewriteRule ^test/([^-]*)$ /test.php?one=$1&two=$2 [L]
Just remove the 2nd /([^-]*) it is making no difference, as you can see here. Removing it will allow for you to access www.example.com/test/1234 too.
You can use:
RewriteRule ^test/([^/-]+)(?:/([^-]*))?$ /test.php?one=$1&two=$2 [L]

RewriteRule and PHP: Adding a new '?param=yes' to the URL

I've got hundred of webpages already created with RewriteRules and PHP
foo.com/section/item/number --> foo.com/section.php?item=number
foo.com/story/number ---> foo.com/story.php?id=number
Now I would like to include an extra parameter for all the webpages
foo.com/section/item/1?param=yes
foo.com/story/34?param=yes
I tried with:
if (isset($_REQUEST['param']))
{
print "IT WORKS!";
}
It works only with 'foo.com' (the 'index.php') with no RewriteRule.
I tried also by adding QSA to the RewriteRule
RewriteRule ^story/([^/]+) /story.php?id=$1 [L, QSA]
What am I doing wrong? Thank you very much.
I'm not able to test, but I think this will do what you need
RewriteCond %{QUERY_STRING !&param=yes
RewriteRule ^/?c/(.*)$ /%1?%{QUERY_STRING}&&param=yes [L]
First this makes sure if a request already has param=yes in it, do nothing, otherwise append either ?param=yes or &param=yes , if the request already has ? in it.

.htaccess writerule to remove question marks and equal mark

today i tried to create .htaccess file that replacing ? and = with / ,
test.php code:
<?php
echo $_GET['myparam'];
?>
.htaccess:
i used this writerule:
RewriteEngine On
RewriteBase /
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
so navigated to: www.web.com/test/myparam/123 (Manually) and the php script is worked,
i changed the myparam value to abc instead of 123 : www.web.com/test/myparam/abc
and then it redirects to 404 not found page...(the server don't know that abc is not directory when integer works when string 404)
!so what i want to do:!
www.web.com/ test .php? inttParam = 1 & strrParam = stringhere & p = 1
TO
www.web.com/ test/inttParam/1/strrParam/stringhere/p/1
and when i use $_GET['p'] it will work.
i changed the myparam value to abc instead of 123 and it didn't work
Well of course it won't work since your rule is matching only numbers in the end:
RewriteRule ^test/myparam/([0-9]+)/?$ test.php?myparam=$1 [NC,QSA,L]
change your rule to this to make it work with anything:
RewriteRule ^test/myparam/([^/]+)/?$ test.php?myparam=$1 [NC,QSA,L]
To make it recursion based generic rule to convert /test/inttParam/1/strrParam/stringhere/p/2 to /test.php?p=2&strrParam=stringhere&inttParam=1`:
RewriteRule "^(test)(?:\.php)?/([^/]+)/([^/]*)(/.*)?$" /$1.php$4?$2=$3 [NC,L,QSA]
RewriteEngine on
# do not affect files
RewriteCond %{REQUEST_URI} !(\..{2,4})$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ index.php?parameter1=$1&%1 [L]
Something like that perhaps?
ok, i navigated to www.web.com/test.php?myparam=123
there is no redirect to www.web.com/test/myparam/123
I don't know if that means that you also want to make a redirection (presumably 301) from the one with GET params to the one with slashes.
If so, I would recommend to use the previous answer from anubhava and handle any 301 redirection with PHP. Actually, you can redirect with "RewriteRule" using the "R" flag, but I admit that I woundn't know how to solve this particular case.
Anyway, I think there is no need, unless old URLs with GET params were working well at SEO and you didn't wan't to lose ranking.

htaccess mod_rewrite:- products.php?cat=1 to /myfolder1/myproduct1/

I've got a url type:- http://www.example.com/products.php?cat=1 which I am able to rewrite to:- http://www.example.com/myproduct1 using the following .htaccess rule:- RewriteRule ^myproduct1$ products.php?cat=1.
What rule would I need to enter if I wanted to rewrite to:- http://www.example.com/myfolder1/myproduct1/?
For any arbitrary path segments a and b in /a/b/, you can use this:
RewriteRule ^([^/]+)/([^/]+)/$ products.php?segment1=$1&segment2=$2
Edit    In response to your comment: Just put good/boy in your pattern:
RewriteRule ^good/boy$ products.php?cat=1
First thing, I assume you want something like http://www.example.com/myproduct2 redirect to http://www.example.com/products.php?cat=2. In this case youd' rather use that rule:
RewriteRule ^myproduct([0-9])$ products.php?cat=$1
Then you can do th same this for your folder, writing something like:
RewriteRule ^myfolder([0-9])/myproduct([0-9])$ products.php?folder=$1&cat=$2
Edit
Then you just have to write:
RewriteRule ^folder1/myproduct1$ products.php?cat=1
RewriteRule ^myfolder/differentproduct$ products.php?cat=2
and so on...

mod rewrite problem with 2 parameters

I'm trying to rewrite the categoy file of my shop system, I also integrated a pagination so I need to rewrite 2 parameters. it almost works, otherwise I wouldn't be posting in here
this is the rewriteurl
RewriteRule ^shop/cat/(.*)/page/([0-9]+)$ /cmstut/shop/cat.php?cat=$1&page=$2 [QSA,L]
This is the url
http://localhost/cmstut/shop/cat/32/page/2
the cat works but not the page and when I print the querystring I get this:
cat=32/page/2
What did I do wrong? I was expecting something like cat=32&page=2 so I could catch the page and show the right page for the pagination.
You’re probably having two rules where the second one looks like this:
RewriteRule ^shop/cat/(.*)$ /cmstut/shop/cat.php?cat=$1 [QSA,L]
This rule will cause that a request of shop/cat/32/page/2 will be rewritten wrong. You need to use a more specific pattern like this:
RewriteRule ^shop/cat/([^/]+)$ shop/cat.php?cat=$1 [QSA,L]
RewriteRule ^shop/cat/([^/]+)/page/([0-9]+)$ shop/cat.php?cat=$1&page=$2 [QSA,L]

Categories