I'have this rewrite condition that redirect all the request of non-existing files to the app.php in parent directory.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^(.*)$ %2../app.php [L]
In the last line (rewriterule) the value of $1 is the path relative to the dir where .htacces is, in order to make it portable ( found here: http://linlog.skepticats.com/entries/2014/08/Using_RewriteBase_without_knowing_it.php ).
That is : calling http://localhost/myapp/public/test123/r.txt I will get test123/r.txt in $1 variable (considering that .htaccess is in public/).
I would like to pass the value of $1 to the app.php, a common solution would be to append it as a query string:
RewriteRule ^(.*)$ %2../app.php?__path=$1 [L]
In this way I have __path as a GET variable (PHP: $_REQUEST["__path"]) , but how it works with other kind of requests like POST (query string should not be there) ?
A more clean solution would be to put $1 in a HTTP custom header but, how can I set a custom header, say MYAPP_PATH to $1 value through .htacess ?
To start with your comment:
I've noticed that the trick ?__path=$1 overrides all the querystring that I pass in the original URL.
You forgot to set the QSA flag.
Second to the core question:
In this way I have __path as a GET variable (PHP: $_REQUEST["__path"]) , but how it works with other kind of requests like POST (query string should not be there) ?
This just works fine. GET-parameters are just popularly called that, they're actually query string parameters and can be passed with any HTTP verb, from GET and POST to PUT and DELETE. So if you POST to a URL with query string parameters you can still read them from $_GET just fine.
Final point, as the above solves all your issues: please do not use $_REQUEST. It's really REALLY bad practice, and may lead to obscure security and stability issues. Just use $_POST, $_GET et al instead.
Related
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
I'm having e commerce website in php. And now moving to fancy urls, url rewriting.
for that i've variable length of parameters with optional variables too.
for example,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
Here, the parameters are optional That means, some variables may or may not participage in the url except id.
here, page=e is fixed and it is not dynamic. ( the reason behind is i've other rewritten rules like ^categories, ^toys etc..
and re-written url should be one of these,
http://example.com/e/23/color-blue/size-xxl/
http://example.com/e/26/color-black/width-500/height-900/
http://example.com/e/56/type-shirt/color-white/height-345/size-xl/
i've tried below with no luck,
RewriteRule ^e/([^/.]+)/?$ product.php?page=e&url=$1 [L,NC,QSA]
i'm getting all $_GET values like this,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
but i'm trying to achieve like this,
http://example.com/e/23/color-blue/size-xxl/width-200/height-100
how can i pass the queries with slashes and not &'s.
that main idea behind above last url is to process the whole fancy url into simple one in php,
and then use those variables in the page script. is there any better solution than this ??
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(e)/([0-9]+)/([^-]+)-([^/]*)(/.*)?$ product.php$5?page=$1&id=$2&$3=$4 [L,QSA]
RewriteRule ^(product\.php)/([^-]+)-([^/]*)(/.*)?$ $1$4?$2=$3 [L,QSA]
I would like to redirect URI into subpage.php and read is as parameter (by $_GET method).
Example 1:
http://homepage.com/wap/show_counties.php?id_county=51&char_county=&lang=en
into
http://homepage.com/controller.php?url=wap/show_counties.php?id_county=51&char_county=&lang=en (??? - can I pass url parameter like this?)
Example 2:
http://homepage.com/accommodation/hostel-star-bratislava
into
http://homepage.com/controller.php?url=accommodation/hostel-star-bratislava
Example 3:
http://homepage.com/en/search-in-page-12.html?ordering=newest&searchphrase=any&searchword=do%25252525252525252525252B700%252525252525
into
http://homepage.com/controller.php?url=en/search-in-page-12.html?ordering=newest&searchphrase=any&searchword=do%25252525252525252525252B700%252525252525 (??? - can I pass url parameter like this?)
EDIT: I would like to make url redirection by .htaccess
Could you help me please?
Regards
Jan Zitniak
You can do this with mod_rewrite. What you want to do can be described like so:
The request uri should be passed to a specific file in a get variable
If there is an original query string, it should be retained
You can do this with the QSA (query string append) flag in mod_rewrite. In addition to that, in the example below I add a condition that checks if the file that is requested does not exist. This prevents the rule from matching itself (controller.php exists) and prevents the rule from working on files that are typically not handled by a controller (js/css/images). Add the following to the beginning of the .htaccess in your www-root:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /controller.php?url=$1 [QSA,L]
See the documentation for mod_rewrite in general and the document about flags specific for more information.
I would like to use mod_rewrite to append a parameter to the end of a querystring. I understand that I can do this using the [QSA] flag.
However, I would like the parameter appended ONLY if it does not already exist in the querystring. So, if the querystring was:
http://www.mysite.com/script.php?colour=red&size=large
I would like the above URL to be re-directed to
http://www.mysite.com/script.php?colour=red&size=large&weight=heavy
Where weight=heavy is appended to the end of the querystring only if this specific parameter was not there in the first place! If the specific parameter is already in the URL then no redirect is required.
Can anybody please suggest code to put in my .htacess file that can do this?
EDIT: tested and modified it, now it works for me
I assume you want to add a default weight parameter if there is no weight at all:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} !weight=
RewriteRule ^script.php script.php?weight=heavy [R,L,QSA]
This is esentially the same what #coreyward answered, but a bit more specific. (R flag, to make the change visible, and the weight parameter does not have to be heavy.)
Hope this helps!
This is a really strange thing to do. It's as though you're setting a default for your system, which is a pretty standard thing to do. The problem is that it's unmaintainable and awkward to be setting that default in your VHost definition or .htaccess file where you're literally configuring Apache.
I advise against doing it this way, but this is a quick and dirty way to do what you're looking for:
RewriteCond %{QUERY_STRING} !weight=heavy
RewriteRule /script.php /script.php?weight=heavy [QSA]
So basically I want users to be able to go to my website with a URL of something like /45678, instead of having to use /?p=45678, so really I just want to remove the variable name. I've tried using mod_rewrite, but it seems that is only for removing the name when the page is visited.
Here is the current code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=([0-9]+=$
RewriteRule ^/$ /%1 [R]
Simply change all of your links to /45678 rather than ?p=45678. Or did I misunderstand you completely? Because what I got from your post is that it works properly, unless you manually access the ?p=45678 where as it stays as ?p=45678.
EDIT:
This is what I am using for http://www.madphp.org/dev/, give it a go, works like a charm for me (it also removes the index.php part). To access your now cleaner URL you would simply explode the $_SERVER['PATH_INFO'] variable to get all of the required parameters within your PHP script.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Have you set up mod_rewrite correctly? If so, you can use variables like simple $_GET variables, in this case you must access $_GET['p'] in PHP.
I did this without using .htaccess, but it does query a database. I wrote this a while ago so it uses PEAR DB, adjust to your database/connection method. I'll just copy my code and let you figure out what changes you need.
$db=connect_db();
$name=substr($_SERVER['PHP_SELF'], 20);
$name=strtolower($name);
$id=$db->getone("select id from user where login='{$name}'");
header("Location: /dragonart/profile?user=" . $id);
If you store your information in a database this may be a nice alternative. The downside is that the the URL is not rewritten and the user is ultimately sent to a page with ending in a $_GET variable.
edit:
Just realized that using my method a simpler method can be used for the answer. Since my solution was used to find the id of a user using their username and then send someone to their profile (which requires the id) a better solution would be something like:
$var=substr($_SERVER['PHP_SELF'], $length);
header("Location: /path/to/page?p=".$var);
where $length is the usual length of the URL without the variable at the end.