After reading tons of SO questions, asking friends and so one, I'm coming here with a strange issue regarding Apache mod_rewrite.
I'm trying to catch http://api.server.com/.../results.php?id=X URL though a RewriteRule.
Quite simple you'll say, I know it, my .htaccess file content is :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1
results.php is quite simple for debugging reasons, looks like
var_dump($_GET);
But this script always return array 0 { }
Shall I specify that I've already tried to clear the flags, and change the (.*) class by others, without effects.
Thanks for your help.
You will need to disable MultiViews option here:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^results/(.*)$ results.php?id=$1 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Your rewrite rule does not match the URL you are using (http://api.server.com/customer/2/results.php).
The correct URL according to your rule and setup is:
http://api.server.com/customer/2/results/123
However, you mention having placed everything in the /2/ folder. If 2 is the ID you are trying to get, it cannot work -- URL rewriting only works with non-existing paths.
Instead you should place your .htacess and results.php file in the customer folder, and use the following URL:
http://api.server.com/customer/results/2
Or change your rule and URL to:
RewriteRule ^([0-9]+)/results$ results.php?id=$1
http://api.server.com/customer/2/results
Related
I store data in text file.
And when user enter in address bar something like
my_syte.com/aaa - (without extension)- I need to file_get_contents aaa.txt file
my_syte.com/bbb - I need to file_get_contents bbb.txt file
Please advise the most powerful way of do it. Apache server.
Thanks
On Apache servers you can use mod-rewrite in .htaccess file:
RewriteEngine on
RewriteRule ^([a-zA-Z]+)$ /$1.txt [L]
if your files can contain - or _ or numbers then use:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.txt [L]
On nginx servers it's more complicated but some of them works with .htaccess. On other servers there may be entirely different approach. It's hard to help you without more informations.
As you said it's Apache, then use examples above. Either edit or create .htaccess file on your webroot (directory which is accessed by domain). First check if it were there (could be hidden) and if it exists then only edit it (add lines at the top).
If it doesn't exist, then create one by yourself.
Can you please give us some insights about your server? Apache nginx?
In Apache, you can achieve that with url rewriting.
Enable mod_rewrite in apache
Put the following line of code in .htaccess on the same location of my_site.com/
RewriteEngine on
RewriteRule ^/foo$ /foo.txt [PT]
to make it generic
RewriteEngine on
RewriteRule ^/*$ /foo.txt [PT]
Maybe I am wrong in sytax based on your specific server configuration. You need to make the best possible regular expression for this case.
My htaccess file code is not working even it is right i found on many website for this each website has same code here it is :-
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^products/([a-zA-Z]+)/([0-9]+)/$ index.php?action=$1&sub_cat=$2
now this thing is not working www.example.com/products/something/3/
anything else i am forgetting please help me.
Because you have a products.php file and your URL looks like www.example.com/products/something/3/, a module called "mod_negotiation" is processing the request before mod_rewrite can. The Multiviews option will allow mod_negotiation to try to "guess" what a request is for, and it sees /products/ in the URL and the file /products.php and assumes that's what the request is for, serves the request via the products.php script and mod_rewrite never gets a chance to do anything.
Solution?
Turn off multiviews:
Options -Multiviews
by adding that option anywhere in your htaccess file.
I'm trying to create nice urls with .htaccess files and have come across a weird issue.
I want to change portfolio.php?id=2 to /portfolio/2/
seems pretty simple solution
RewriteRule ^portfolio/([0-9]+)/$ /portfolio.php?id=$1 [L]
this does redirect to the correct script but when i try and run <?=$_GET['id'];?> it is undefined. but if change the script to something that does not equal the fake directory it works.
RewriteRule ^portfolio/([0-9]+)/$ /portfolioitem.php?id=$1 [L]
and just to make sure that it wasn't being caught by any other rules I tested this
RewriteRule ^portfolioitem/([0-9]+)/$ /portfolioitem.php?id=$1 [L]
and again it failed to pick up the id paramater!
any ideas?!
Cheers
This sounds suspiciously like a Multiviews related problem coupled with some PATH_INFO. The Multiviews option is part of mod_negotiation and it will try to match a requested URL path to a file path. It sees:
/portfolio/2/
And sees that there's a /portfolio.php file in the filesystem and assumes that this is what you want (which it is, but not in the same way). I'm willing to bet that instead of looking at $_GET['id'], which is blank of course since there are no GET params, if you look at $_SERVER['PATH_INFO'], you'll see it set to /2/. This is equivalent to going to:
/portfolio.php/2/
where the /2/ part gets passed to portfolio.php as part of the PATH_INFO. And since mod_negotiation is further up in the processing pipeline than mod_rewrite, your rewrite rules never get applied.
Try turning off multiviews. You can do this in your htaccess file using the Options directive (assuming your host has allowed Options):
Options -Multiviews
I am on a website. the URL reads something like https://somesite.com/serve
I need to tell the name of the page that is serving me.
Like index.html, index.htm, etc...
It is fundamentally impossible for you find that out.
You want to know the file that is being served. Well probably you are facing url rewrite with htaccess or other techniques. To tell which file it is probably is impossible if only you manage to get framework (if it is framework) in which the page is made. Then you can read in documentation which is the file to which the requests are aimed. Most frameworks will have one or several of these files. For example codeignighter will have only index.php, while symfony 2 will have app.php and app_dev.php (and others if you want different environments). But normaly you cant know which file serves your request if url rewrite is made.
As mentioned #Dale you cant also beleave what urls say. Because you cant stick some extension at the end for it to look as file. Sometimes you can notice .php or more often .html / .htm at the end.
I'm not entirely sure what you mean. But I'm certain you can find it with $_SERVER (Documentation can be found here)
Good luck
If you just trying to find out the page name then you can do this:
<?php
$currentFile = $_SERVER["PHP_SELF"];
$parts = explode('/', $currentFile);
echo $parts[count($parts) - 1];
?>
probably then you have a .htaccess with rewrite rules which looks something like:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Else you probably would have query string instead which probably looked something like:
index.php?c=products&m=view&id=345
(Assuming server supports .htaccess, mod_rewrite, mod_headers)
Temporarily use .htaccess RewriteRule to reveal all or matching filenames in a header.
Example tags all php file headers with filename:
<IfModule mod_rewrite.c>
RewriteEngine on
<IfModule mod_headers.c>
RewriteRule .*\.php$ - [E=FILENAME:$0]
<FilesMatch ".php$">
Header set MY-COOL-FILENAME "filename=%{FILENAME}e"
</FilesMatch>
</IfModule>
</IfModule>
Links
Setting a filename inside the header with htaccess
View HTTP headers in Google Chrome?
I have read more than 3 questions here in stackoverflow about this, but all of them has one of the below problems:
It redirects php to htm (not found appears, I already have php files in place)
When you type *.htm, it will be redirected to *.php (I want visitors to see *.htm not *.php)
Let me boil it down for you guys: I need to type index.htm and apache redirects that to .php but in the address bar I still want to see .htm not .php
Any help or suggestion would be appreciated.
RewriteEngine on
RewriteRule ([^/]*)\.htm$ $1.php [PT]
Please review http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule for all of the available options, including other flags you may want to include - such as QSA.
Note that the RewriteEngine on line (as well as Options FollowSymLinks - see the docs linked to above) is necessary if you don't already have it elsewhere. You may also need AllowOverride FileInfo in your primary Apache configuration to enable the use of .htaccess files.
Note that the PT flag is not strictly necessary, as it is the default if Apache determines it can fulfill the request without a redirect.
Here is another result I just found from a Google Search that shows pretty much the same thing I'm describing here: http://corz.org/serv/tricks/htaccess2.php (specifically shows some rules around the .htm -> .php translation.)
Hi it's easily done: In your .htaccess you will have a redirect like this:
RewriteRule ^(.*).htm$ $1.php [QSA]
RewriteRule ^(.*).html$ $1.php [QSA]