I have an issue in posting php data.
I have small website and I made a php file cat.php.
Normally to post id it should be like that cat.php?id=.
I'm asking if I can do it like this cat-ID.php.
I think the following should work for your scenario:
RewriteEngine on
RewriteRule cat-([0-9]+)\.php cat.php?id=$1 [L]
This would rewrite /cat-3.php to cat.php?id=3 and /cat-666.php to cat.php?id=666 and so on.
Your first example is POST, not GET (e.g. cat.php?id=5 would be $_GET['id']). You can, though, use htaccess rewrites to create URLs something like cat-[id].html instead, which is cleaner. See examples: http://httpd.apache.org/docs/trunk/rewrite/remapping.html
Update:
Mod Rewrite example. This would be in .htaccess:
RewriteEngine On
RewriteRule cat-([0-9]+)\.html cat.php?id=$1 [L]
Then in PHP, you could:
<?php
echo $_GET['id'];
?>
And you should be able to hit /cat-5.html and see 5 echoed.
Related
I'm trying to do simple trick with .htaccess file, but with without success.
Generally I have PHP script that makes dynamically generated signatures and link looks like this:
example.com/signature/generate.php?name=%SomeUserName%
where %SomeUserName% is simply username e.g. Patison
and I'm trying to get:
example.com/signature/%SomeUserName% or (if necessary)
example.com/signature/generate/%SomeUserName%
Last code that I'm tried with no success:
RewriteEngine On
RewriteRule ^/signature/generate.php?name=(*) /signature/$1 [L,NC]
.htaccess is hard to understand for me.
So I have one more question. When someone use this link on another site he will render an image or it will work only on mine?
This is the sort of approach you need:
RewriteEngine on
RewriteRule ^signature/([^/\.]+)?$ signature/generate.php?name=$1 [L,NC,QSA]
So anybody going to signature/levi will see signature/generate.php?name=levi
I've used RewriteRules before, but usually only copying and altering from a CMS framework, so I've not had the fun of writing them myself from scratch. However, using my basic knowledge of them, I've put together the following .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^post/(.*)$ post.php?id=$1 [L]
RewriteRule ^tag/(.*)$ tag.php?id=$1 [L]
RewriteRule ^more/(.*)$ subpage.php?id=$1 [L]
In case I've done something really wrong, the effect I'm aiming for is:
http://example.com/post/testpost
is rewritten as
http://example.com/post.php?id=testpost
however, I cannot for the life of me get the id to show up. The server redirect to the correct page (post.php), and all seems to be fine, but I cannot call the id from $_GET. I've run a var_dump of $_GET, and the array is empty, so I know I'm not just misspelling it in my php. If I manually visit the post.php page as the RewriteRule should output, the variable shows up fine. The code on post.php is:
<?php
//this function performs sanitisation and returns the HTML page contents
echo PostViewer($_GET['id']);
//this is just for testing purposes
var_dump($_GET);
?>
Any help would be really appreciated, thanks in advance!
Add "QSA" after "L" eg. [L, QSA]
what's happening is your RewriteRule is using the GET and then not re-appending it for your script to use. QSA = Query String Append. Does exactly what it says on the tin.
So I want to use mod_rewrite to transform:
random_name.com/main.php?user=$user
into
random_name.com/$user
(the $user is a php variable added onto the url, so it can be any name such as andy or rebecca, names like that)
and my code for the .htaccess is:
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/?$ main.php?id=$1 [NC,L]
But this doesn't seem to work for some reason. I've read up on the tutorials but they're really complicated and it seems like this would do the trick, but it doesn't. I'll appreciate it if anyone who has experience with mod_rewrite would give me a few pointers.
You cannot use mod rewrite to transform random_name.com/main.php?user=$user into random_name.com/$user.
You have to do it manually in all the links on your site.
After that you may use mod rewrite for the reverse transformation, which will make /main.php?user=$user out of /user request
Try this:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)$ main.php?user=$1 [NC,L]
I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?
I want it to be Facebook style...Where "username" is the username chosen by idu = x.
There are multiple ways to solve this problem, but here's one that always suits my needs.
Guide all your URL requests through the index.php first and resolve the request in your PHP code second.
1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.
2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.
Good luck and beware of URL injections using this method.
You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.
You can try to use this mod_rewrite pattern (add it to the .htaccess):
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1
you have to write a clean URL in your .htaccess file like :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1
Put the following in your .htaccess
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]
The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.
i got a Q regarding mod_rewrite with php.
currently my userprofile php page has the following link to determine a user:
domain.com?username=john
i would need to convert this url into like this:
domain.com/john.
how can i do this in mod_rewrite??
Something like:
RewriteEngine On
RewriteRule ^domain.com/(.*)$ domain.com/profile.php?username=$1
I found the solution. The initial problem was my profile php page has the following link: domain.com/profile.php?username=john
instead of this, i would need to make it easy to read url like this: domain.com/john.
what i did is:
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ /profile.php?username=$1 [L]
I'm not sure if this has any security breach but do leave your feedback