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 !¶m=yes
RewriteRule ^/?c/(.*)$ /%1?%{QUERY_STRING}&¶m=yes [L]
First this makes sure if a request already has param=yes in it, do nothing, otherwise append either ?param=yes or ¶m=yes , if the request already has ? in it.
Related
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
To give some context, I initially started out with my api looking like this:
http://myserver/api.php?action=projects
In my api.php file I have a simple if statement to detect the action, and then react accordingly:
if (isset($_GET["action"])
{
switch($_GET["action"])
{
case "projects"
dostuff();
break;
case "otherstuff"
dootherstuff();
break;
...
}
}
However, I of course wanted to clean this up, so I could go to this url:
http://myserver/api.php/projects
and get the same outcome. I was able to accomplish this by reading up on a previous SO post:
Using Clean URLs in RESTful API
And ultimately got it working by making an .htaccess file in my root directory and using the rewrite engine like so:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L]
This worked great! Except for one problem, because I am using the clean URL, it no longer seems to recognize the GET parameter exists.
I've found I can use
$_SERVER['REQUEST_URI'];
And then parse the URL to get my parameters. I don't see how this could work out though once I start introducing multiple parameters. Is there something I'm missing here, or do I need to look at it a different way and change how I format the parameters instead?
You can use these rules:
RewriteEngine On
RewriteRule ^(?:action|api\.php)/([^/.]+)/?$ api.php?action=$1 [L,NC,QSA]
Change to this:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L, QSA]
You need the QSA parameter to keep the querystring intact. So now this is possible:
/action/projects/?my=name
Or you can do this:
RewriteEngine On
RewriteRule ^action/([^/\.]+)/([^/\.]+)/?$ api.php?action=$1&method=$2 [L, QSA]
RewriteRule ^action/([^/\.]+)/?$ api.php?action=$1 [L, QSA]
Now you will have these urls:
/action/projects/add/
/action/projects/
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.
I've been searching all over the web and haven't yet found any solution to this issue. I'm hoping you could shed some light on the situation.
I have my index file set up like this:
<header></header>
<div id="main">
<?php
if(isset($_GET["p"])) $p = $_GET["p"];
else $p = "home";
if(file_exists("pages/{$p}.php")) include("pages/{$p}.php");
?>
</div>
which makes me load my pages with a ?p=contact href.
Say I would like to display a users profile. I'd then create a subfolder in my "pages" folder, making the relative path pages/users/profile.php, thus the href ?p=users/profile&uid=5. But that leaves an ugly URL (as well as SEO rating).
How would I rewrite that URL to look like /users/profile/5?
EDIT:
I've tried the following, resulting in HTTP 500:
RewriteRule ^([^/]*)/([^/]*)$ /?p=$1&uid=$2 [L]
EDIT: My .htaccess file, located directly inside root folder: http://pastie.org/2268239
Line 338 is where I want to achieve this (currently just a comment).
Simplest answer for both your situations would be to add this in your .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php?$1 [L]
This will redirect all traffic on your domain to your index.php file.
You could then determine what to do in your script using the uri in $_SERVER["REQUEST_URI"]
I achieved the desired effect by adding these three lines:
RewriteRule ^([a-zA-Z]+)$ index.php?p=$1 [L]
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)$ index.php?p=$1/$2 [L]
RewriteRule ^([a-zA-Z]+)/([0-9]+)$ index.php?p=$1&uid=$2 [L]
This allows me to access /contact, /users/index and /users/profile/5.
I'm not a php guy, but I tried this with my rewrite.
RewriteRule ^/(.+)/([^/]+)$ /index.php?p=$1&uid=$2 [L]
In this case, for the p parameter, you're looking for all chars up to the last slash, so the first part of this takes anything, otherwise it's going to stop at the first slash (users instead of users/profile).
Then it looks for a slash and keep (not-slash). The (.+) will be greedy, so it will go up to the last slash before the end.
Then it occurred to me the last part doesn't need to avoid slashes. Since the first part is greedy, the explicit / slash is going to BE the last slash. So it's even simpler:
RewriteRule ^/(.+)/(.+)$ /index.php?p=$1&uid=$2 [L]
I like the .+ to require something, at least when first figuring these out. If later you know they can be optional, you can do .*, but usually that ends up being a different page or a different rule.
These rules do expect all urls to be in this format, which is what you're asking. But maybe it's a little too grabby, so it could exclude urls that really have a .htm or .php or whatever.
RewriteRule ^/(.+)/([^.]+)$ /index.php?p=$1&uid=$2 [L]
This looks for anything up to the last slash, then anything without a dot in it. If it has a dot, this won't apply. So if it's a "regular" url, this will leave it alone. This might help with the 404 problem, in case the 404 page is getting caught by this.
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.