I'm currently making a website (with PHP and a database) and there something I'm wondering about.
Can you get an ID from a URL like this for example egh95.com/u1 or egh95.com/t52-it-s-a-new-day?
Like I only want the int to be my ID. I tried to use $_Server["REQUEST_URI"] and strip all the characters and symbols from the URL, but the int I got, didn't work to get the information out of the database.
I know how to use $_GET["id"] to get the int from the URL (that's the only thing that works atm), but I actually don't want the URL to be ?=id1 or something like that.
I'd just use a basic regex, if that's the case.
$url = 'egh95.com/u1';
preg_match('/^.*\/u(?P<data>.*)/', $string, $matches);
echo $matches['data'];
$matches['data'] contains the number after u, in this case 1. Here are some docs for regex in PHP, if you're interested.
If you use Apache, you can use ModRewrite to implement user-friendly URLs. It is easy way to achieve what you wrote about with small amount of work. Then write in .htaccess file following lines: (Assuming you want to process the id in index.php file)
Options FollowSymLinks
RewriteEngine On
RewriteRule "^u(.*)$" "index.php?id=$1"
Related
I am creating a simple redirect tool for myself in PHP. I'm going to store codes that map to their respective URLs in an associative array, and then redirect anybody who inputs this code to their URL.
I am using query strings to GET the input (the name of the variable and the value, which is the code), and I already know how to make the redirect part work with PHP. The issue is, I don't want my users to have to type in a question mark, the name of the variable, and then an equals sign. I would like for them to simply type in a path that will be converted into a query string, so that PHP can process the redirection.
I was wondering how I could turn a path (an arbitrary URL path) into a query string of an existing file. I was able to find the opposite task quite easily on Stack Overflow (.htaccess - Rewrite query string and redirect to directory) but I want to be able to pass any arbitrary URL path (that does not exist in the directory) into an existing file's query string. I found an example of what I wanted (URL rewrite for converting path to querystring), but it was for multiple query strings and in this case, I only need one. I don't think the last question I referenced was successfully answered either.
How can I redirect
https://www.example.com/r/blahblahblah
into
https://www.example.com/r/index.php?r=blahblahblah
using .htaccess, so that I can parse the query string in my PHP file?
Something like this:
RewriteRule ^(.*)$ index.php?r=$1 [NC,L,QSA]
On this URL:
example.com/r/blahblahblah
From index.php:
print_r($_GET);
Will return this:
Array ( [r] => blahblahblah )
(Edit to mention this assumes .htaccess is in the /r directory)
RewriteEngine On
RewriteRule ^r/([a-zA-Z0-9]+)$ /r/index.php?r=$1
I have been trying & searching alot but couldn't find any solution for i have tried many things in .htaccess
My website's Url
mysite.org/torrents.php
To this
mysite.org/Browse
This
mysite.org/torrents.php?parent_cat=Movie
to this
mysite.org/Movie
This
mysite.org/account-details.php?id=737
to this
mysite.org/user/username
Your links won't all work properly, i.e. the /user/username one won't work to an ID number, you could pass the username through then do a lookup based on the username, but htaccess won't know that username "tim" is id number "123"
This should work:
RewriteEngine On
RewriteRule "user/([0-9]*)" account-details.php?id=$1
RewriteRule "category/([a-zA-Z0-9_-]*)" torrents.php?parent_cat=$1
RewriteRule "browse" torrents.php
RewriteRule "" torrents.php
With that I've separated out the categories into a subfolder called 'category' this makes it far easier to allow any category name you want.
For the regular expression matches in the RewriteRules theres an excellent free program called 'Expresso' that will help you build up regular expressions and test them against a list of values to see if they match, I'd recommend searching for it and getting a copy as it's very handy.
Also for the username part if you wanted to do username instead of user ID numbers, then swap:
RewriteRule "user/([0-9]*)" account-details.php?id=$1
to this:
RewriteRule "user/([a-zA-Z0-9_-]*)" account-details.php?username=$1
Try using this. it is a htaccess generator. If this doesn't work you will need to check you php.ini settings to see if it allows this type of link.
I want to know how I can pass parameters between pages through the URL without having to add variables eg:
mydomain.com/file.php?var1=val1&var2=val2&...varN=valN
I want to use it as follows:
mydomain.com/file.php?val1-val2-...-valN
I also see in some website the URL is in the following format
mydomain.com/file/####
which redirects to another page without changing the URL as if this is the URL to the file.
You should use .htaccess
Here's an example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9-_]+)/([a-zA-Z0-9-_]+)/?$ index.php?var1=$1&var2=$2 [NC,L]
This basically means that if the URL is formatted according to the regular expressions above (number - slash - alphanumeric,dashes or underscores) than the content should be displayed from index.php while passing the file two parameters called var1 and var2, var1 having the value of the number and the second having the value of what's after the first slash.
Example:
mysite.com/20/this_is_a_new_article/
Would actually mean
mysite.com?var1=20&var2=this_is_a_new_article
Of course, in your index.php file you can simply take the values using
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];
Cheers!
Your third example is an example of how REST identifies a server side resource. What you are talking about here sounds very much like REST will do what you want. I would suggest starting here (http://en.wikipedia.org/wiki/Representational_State_Transfer#RESTful_web_services).
I am using a script to check links on a given page. I am using simple html DOM to parse the information into an array. I have to check the href of all the a tags to find if they contain a file or something like # or JS.
I tried the following without success.
if(preg_match("|^(.*)|iU", $href)){
save_link();
}
I dont know it my pattern is wrong or if there is a better method to complete this function.
I want to be able to detect if $href contains .com .php .file extensions. This way it will filter out items like # "function()" and other items used in the href attribute.
EDIT:
parse_url will not work stop posting it. The value # returns as a valid url like I stated above I am trying to look for any string followed by .* with no more than 4 chars following the .
I believe that the function you're looking for is parse_url().
This function will take a URL string, and return an array of components, which will allow you to work out what kind of URL it is.
However note that it has issues with incomplete URLs in PHP versions prior to 5.4.7, so you need to have the very latest PHP to get the best out of it.
Hope that helps.
See http://php.net/manual/en/function.parse-url.php
I'm assuming you don't want to match fragments (#) because you are not concerned with following internal anchors.
parse_url breaks up the different parts of the url into an array. You can see the path component of the URL in this array and run your check against that.
You can use parse_url() , like this :
$res = parse_url($href);
if ( $res['scheme'] == 'http' || $res['scheme'] == 'https'){
//valid url
save_link();
}
UPDATE:
I've added code to filter only http and https urls, thanks to Baba for spotting this.
I'm writing a simple URL routing code based on using mod_rewrite to pass the URI as a GET parameter, like Drupal does. So I have the rule:
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
And the URL http://www.example.com/test/1 would give me "/test/1/" passed as value $_GET['q'], instead of the usual index.php/test/1 and having to extract that from $_SERVER['REQUEST_URI'].
The thing is, the mod_rewrite QSA flag allows me to still use query strings normally, which I find very useful for parameters like filters and pagination, like "/products/category/?pg=1".
Will this work the same on Nginx and Lighttpd servers? I'd like my code to be portable.
Thanks.
No, in either case you'll have to translate your rules into each server's specific syntax. Some links to get you started:
http://forums.digitalpoint.com/showthread.php?t=187965
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
https://serverfault.com/questions/24243/nginx-support-for-htaccess-rewrite-rules-differences-from-apache