Unable to append query to url ending like /?user - php

www.something.com/?user, where "user" is a php file, is there any way to add query string parameters to it?
Have failed with:
www.something.com/?user?id=1
www.something.com/?user&id=1
www.something.com/?user/id=1
Have seen this topic: Get URL query string
but this is not exactly what I'm after...thanks

The way to do it with the php extension intact would be:
www.something.com/user.php?id=1
You then use:
$userID = $_GET['id'];
If you want to drop the PHP extension, you can do this with a rewrite in the htaccess: This is pretty generic as it depends on your web-server, but something like:
RewriteRule ^www\.something\.com/user\.php$ /www.something.com/user?&%{QUERY_STRING}
Or you could just drop using the PHP extension on all of the files to keep it neat using something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
This will rewrite all of your php extensions so this:
something.com/someFileName
Will automagically be read as
something.com/someFileName.php
To which you can still use:
something.com/someFileName?someQueryParameter=someValue

Related

changing php function calling URL with mod_rewrite

I've already search for an answer here, but couldn't find an exact answer.
I've got a php application (it uses CodeIgniter) which is connected to our companys Management Database. The application provides information out of the database in xml form so that our internal mediawiki's can receive those and build Info-Boxes (as example) out of them.
I have the following link to my data in xml format:
https://example.com/controller/function/databaseID/short_name or
https://example.com/App/makeInfoBox/258/Applicationname
which contains Infromation as following:
-<infobox>
<id>258</id>
<short_name>Applicationname</short_name>
<long_name>Long Applicationname</long_name>
<app_number>334</app_number>
<status>End of life</status>
...
</infobox>`
I now want mod_rewrite to change the url to:
https://example.com/appInfoBox.xml?id=258&short_name=Applicationname
I've got something like this:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)\&short_name=(.*)$
RewriteRule ^appInfoBox\.xml$ App/makeInfoBox/%1/%2
I'm really not good in mod_rewrite and this code I got is based on a older version of a used .htaccess file.
Any Suggestions? Thanks!
You must capture the relevant parts of the request (...) and use this in the substitution $...
RewriteRule ^App/makeInfoBox/(.+?)/(.+)$ /appInfoBox.xml?id=$1&short_name=$2 [L]
Use the following directives in the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/App/makeInfoBox/([0-9]+)/(.*)$
RewriteRule ^(.*)$ /appInfoBox.xml?id=%1&short_name=%2 [QSA,R=302]
The .htaccess file should be present in the root folder of example.com.
The RewriteCond directive will check whether the request matches the pattern /App/makeInfoBox/(any-number)/(any-number-of-characters).
IF this is true the RewriteRule directive will map the request to example.com/appInfoBox.xml?id=(any-number)&short_name=(any-number-of-characters) using the QSA flag.
The R=302 flag will cause an external redirect and the end user will be able to see the new URL with query string on the browser.
If you don't want to display the URL with query string to end users, just remove the R=302 flag.
Using your example, now if you will access https://example.com/App/makeInfoBox/258/Applicationname it will redirect to https://example.com/appInfoBox.xml?id=258&short_name=Applicationname

.htaccess read from MySQL

I have a simple .htaccess file for vanity URLs, like this:
RewriteEngine On
RewriteRule ^example-category/?$ category.php?id=1 [NC]
The idea being that all category URLs instead of looking messy redirect to plain English versions.
My categories are stored in a MySQL database. So I'm wondering if it's possible (and advisable) to have the .htaccess file read directly from the database. This way if I add or delete a category the vanity URLs will update accordingly without manual intervention.
Is this a good idea? And if so how can you get a .htaccess file to read from MySQL?
Thanks
You can use the category name instead rather than using the ID. Here is a quick example.
htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILEMAME} !-f
RewriteCond %{REQUEST_FILEMAME} !-d
RewriteRule ^([^/]+)/?$ /category.php?name=$1 [NC]
This will take a URL like
http://example.com/books/ and route it to your php file.
Then in your php file you can get it with something like this to use the name.
<?php
$cat = $_GET["name"];
$query = "SELECT * FROM categories WHERE name='".$cat."'";
?>
That's not a complete PHP example. Just showing you how you can us the name instead of the ID. Note: I didn't take in consideration for security via mysql etc.

htacess how to convert two query strings to one url

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^news/([^/]+) /casino2/newsdetail.php?newsid=$1 [NC]
Currenty using the above code to conver http://localhost/casino2/newsdetail.php?newsid=3 to
http://localhost/casino2/news/3 now what problem is i am trying to make one url out of two query strings like if url is localhost/casino2/newsdetail.php?newsid=3 & pagename=article the url should be localhost/casino2/news/article is it possible to do so ?
To start with, those rules appear to do the opposite of what you say you want - i.e. they're rewriting "/localhost/casino2/news/4711" to "/casino2/newsdetail.php?newsid=4711".
Provided this is what you really mean, what you need to do is to save two items into two variables, instead of just one into one variable, like this:
RewriteRule ^news/([^/]+)/([^/]+) /casino2/newsdetail.php?newsid=$1&pagename=$2 [NC]
If the newsid is always 1, so that you don't need to pass it to the php, you simply create it in your replacement, like this
RewriteRule ^news/([^/]+) /casino2/newsdetail.php?newsid=1&pagename=$1 [NC]
This would create the following rewrites:
/news/article -> /casino2/newsdetail.php?newsid=1&pagename=article
/news/somethingelse -> /casino2/newsdetail.php?newsid=1&pagename=article
But if you want to be able to select different newsids, you need to pass it in somewhere - either as part of the URL or as a querystring.
Just to be clear, the way the rules are applied is
RewriteRule "What the URL looks like in the browser" "What my web server expects"
If you have problems, I highly suggest you turn on a lot of debug logging, it'll be a great help. The relevant config lines are
RewriteLog /path/to/logfile
RewriteLogLevel 3

Use SO's URL format for $_GET

StackOverflow has a very neat, clean URL format. It looks the same as a directory structure, but there can't be a directory for each question on here! My question is this:
How can I get http://www.site.com/sections/tutorials/tutorial1, for example, to stay like that in the address bar, but convert it to a $_GET request for PHP to mess around with?
I could use a .htaccess file, but I don't want the URL being rewritten - I'd like it to remain clean and friendly. Is my only option here to use PHP's string splitting functions to get some pretend $_GET data?
Thanks,
James
What about this, using .htaccess to split the URL up, the URL won't change but instead point to index.php with various $_GET variables, this could could be increased to cover more URL sections.
# turn rewriting on
RewriteEngine on
# get variables in this order, [object], [object,action], [object,action,selection]
RewriteRule ^([^/\.]+)/?$ /index.php?object=$1 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2 [L,NC,QSA]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?object=$1&action=$2&selection=$3 [L,NC,QSA]
A PHP Rest framework could do this for you, so I refer you to this question. Most of the frameworks won't load the data from $_GET, but will offer a similar and equally convenient way to read it.
It's actually a RESTful way of building your URI's. Not only SO applies this pattern. I recommend to not re-invent the wheel by taking a look at this question.
In addition you could switch over to a RESTful framework such as CakePHP or CodeIgniter, which are configured by default to use the RESTful pattern.
$_GET does not contain the path compontents from the URL, only the parameters that eventually follow the ?. You could use
$parts = explode('/', pathinfo($_SERVER['REQUEST_URI'], PATHINFO_DIRNAME));
var_dump($parts);
However it seems you should have a read on URL rewriting e.g. with mod_rewrite. "I don't want the URL being rewritten - I'd like it to remain clean and friendly" ... The rewriting happens on the server. The user never sees the "ugly" result.
If you don't want to use mod rewrite the best solution would be using regular expressions agains the $_SERVER['REQUEST_URI'] variable.
i.e:
preg_match('|/(.*)/(.*)|', $_SERVER['REQUEST_URI'], $match);
$_GET['param1'] = $match[1];
$_GET['param2'] = $match[2];
If you want to setup a capture all php script. IE if the script request doesn't exist use a default script, use mod-rewrite to redirect everything to one script i.e. the zend framework (and most of the PHP MVC framework) use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I think that could be a bit cumbersome.

Easy mod_rewrite - So I'll never have to think about it again

Not sure how you'll take this question but...
Whenever I try to make my URLs look pretty I always end up messing around for too long and it's simply not worth the trouble. But the end effect is good if it were a simple task.
So what I want to do is create a method which in the end would achive something like...
index.php?do=user&username=MyUsername //This becomes...
/user/MyUsername //...that
index.php?do=page&pagename=customPage //And this becomes...
/page/customPage //...that
index.php?do=lots&where=happens&this=here //This also becomes...
/lots/happens/here //...that
index.php?do=this&and=that&that=this&and=some&more=too //And yes...
/this/that/this/some/more //This becomes this
So then I just make a nice .htacess file that I'll never have to look at again. Everything will be better in the world because we have pretty URLs and my head didn't hurt in the making.
You can use a different approach of throwing the url in a single parameter, and parse it in your application.
So the apache rewrite rule would look like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
which will convert your urls as follows:
/user/MyUsername => index.php?q=/user/MyUsername
/page/customPage => index.php?q=/page/customPage
...
In your app, you then have a $_GET['q'] variable, which you can split by '/', and have your arguments in order. In PHP it would be something like:
$args = explode('/', $_GET['q']);
$args will be an array with 'user', 'MyUserName', etc.
This way you will not have to touch your .htaccess again, just your app logic.
For /user/MyUsername ==> index.php?do=user&username=MyUsername and /page/customPage ==>
index.php?do=page&pagename=customPage, you can use:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?do=$1&$1name=$2 [L]
But I don't think you can write a catch-all rule for /lots/happens/here and /this/that/this/some/more because you need to tell mod_rewrite how to translate the two urls.
Remember, mod_rewrite has to translate /lots/happens/here into index.php?do=lots&where=happens&this=here and not the other way around.
The best approach would be to delegate your application to generate the “pretty URLs” as well as parse and interpret them and to use mod_rewrite only to rewrite the requests to your application with a rule like this one:
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This rule will rewrite all requests that can not be mapped directly to an existing file to the index.php. The originally requested URL (more exact: the URL path plus query) is then available at $_SERVER['REQUEST_URI'].

Categories