PHP read the URL for search (Apache) - php

I need in the
url: domain.com/search/hola
to ignore this hola (so go to /search/index.php) but have this hola in a php variable.
For example i type domain.com/search/google in my browser, then go to domain.com/search/index.php with the code:
$search = $_GET['url']; //(obviously, this doesn't works)<br>
print_r($url);
Something like that: http://example.com/ put what you put go to the same site.

You can do this with a RewriteRule.
Install the module mod_rewrite into your Apache. Depending on your server and OS, instructions may differ, but most of the time it's already installed.
Check that AllowOverride All is specified in your Apache's site configuration to allow .htaccess files.
Create a .htaccess file in the root folder of your webserver.
RewriteEngine On
RewriteRule ^/search/(.*) /search/index.php?param=$1 [L,QSA]
If you do any changes in your apache configuration or install a new module, you will have to restart the apache service. Changes in a .htaccess file do not require a restart.
HowTo http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

Cannot run project by CodeIgniter

I have problem with my project. Because this is a secret, I can not publish my site here. I am sorry for this and this is my case.
I have a project based on the CodeIgniter framework, I run it successfully on localhost but when I use SVN to commit this project to my hosting. It do not run. I am trying to delete all file and commit only index.html with content is "test". Browser show result is "test". I am trying to commit this project again but result still is "test".
However, if I type index.php after my url "http://myaddress.com/index.php", I see my result which I would like. I have tried to config file "config.php" remove "index.php" in $config['index.php] = '' but nothing changes.
From the CodeIgniter's doc, check the "Remove the index.php file" on this page: http://www.codeigniter.com/userguide2/general/urls.html
And just to remember, have you enabled Apache's mod_rewrite?
Add .htaccess file with the following rule in root folder,
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
To make .htaccess work , you have to enable apache module mod_rewrite
If you have access to server, you can type the following command in the terminal (for linux servers, this example specific to ubuntu)
a2enmod rewrite
Restart apache2 after
/etc/init.d/apache2 restart
or
service apache2 restart
If you have not access to server, please contact server administrator.

htaccess not working on QNAP NAS

I have a QNAP NAS with Apache correctly installed. Some pages are linking fine except for ones using any RewriteRule. All other pages are linking correctly to mysql and displaying fine its the ones with any RewriteRule, these are showing up as a 404 error like this:
The requested URL /share/CACHEDEV1_DATA/Web/clients/hembury4x4/couk/view-sitemap.php was not found on this server.
URL = http://192.168.1.210/Web/clients/clientname/sitemap.html
FILE= http://192.168.1.210/Web/clients/clientname/view-sitemap.php
My rule is quite simply: RewriteRule ^sitemap.html$ view-sitemap.php [NC,L]
I have copied all the site files from my computer where the redirect was working perfectly. What do i need to add on my htaccess file?
Thanks in advance
First, apache should have rewrite module. It should present in the output of httpd -M command.
Then you should allow .htaccess files. This can be made by adding AccessFileName .htaccess directive (if it's absent) to your httpd.conf file. Also check you have this section:
<Directory />
AllowOverride All
</Directory>

.htaccess url rewrite not passing parameter - could there be apache settings somewhere that is preventing this?

Okay, so this problem has completely stumped me and the other devs I work with. Here is the rundown:
I have a local dev environment setup with Mac Apache2 pointed at /Users/myusername/Sites/
Within /Sites I have two folders, /site-1 and /site-2, both of which have virtual hosts pointed at them site-1.dev & site-2.dev. Both site-1 and site-2 are running local installs of PerchCMS.
Within /site-2 I have an .htaccess file which I am trying to set up a URL rewrite that takes the URL /detail/slug-here and translates it into /detail.php?s=slug-here
I have tried the following rewrites (at the suggestion of PerchCMS support) and both have failed to pass the s param:
RewriteRule ^detail/([a-zA-Z0-9-/]+)$ detail.php?s=$1 [L]
RewriteRule ^site-2/detail/([a-zA-Z0-9-/]+)$ /site-2/detail.php?s=$1 [L]
Additional info:
Yes mod_rewrite is enabled in apache... in the same .htaccess file it totally works if I do a simple rewrite like this...
RewriteRule dangerzone.html index.php
One odd behavior that I've noticed is that if I remove everything from .htaccess I can still pull up detail.php by pointing my browser at /detail/test-item-1...(yes I have restarted my server) so its behaving as if there is still some sort of rewrite in place and loading detail.php sans param just as it continues to do with the rewrite in place - is this a clue that there is something off somewhere else in my server config? Note, RewriteRule dangerzone.html index.php does NOT work once it is removed from .htaccess.
Have this code in your site root .htaccess (inside /site-2/):
Options -MultiViews
RewriteEngine On
RewriteRule ^detail/([a-zA-Z0-9/-]+)/?$ detail.php?s=$1 [L,QSA,NC]
Important is to turn off MultiViews options here. 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.

Zend SetEnv in .htaccess not working

I installed Zend on my ubuntu homeserver. In my .htaccess file i have the following code:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
When i echo APPLICATION_ENV in my index.php in the public folder, APPLICATION_ENV is not set.
What am i doing wrong?
Mod rewrite is enabled in apache.
In order to use SetEnv within a .htaccess file, I believe you need to set...
AllowOverride FileInfo
...within the relevant virtualhost directory block. (And then restart the httpd service as per usual.)
Additionally, depending on how you're running PHP, it's possible that such information is being stripped out. (e.g.: suexec will effectively remove all non-HTTP* environment vars.)
First You need mod_env installed (and mod_rewrite)
Double check if Apache has the module(s) loaded with either:
apachectl -l
or
httpd -l
the command might be different according to OS distribution i believe.
For the .htaccess file to work you need AllowOveride All which can be set in different configuration files (and "parts" depending on the way your OS as Apache configured. Your best option is to find out if it is a htaccess file issue or an environment one. Set anywhere in main configuration file:
SetEnv TESTME foobar
restart Apache and echo TEST from a PHP script. If you got it, debug the .htaccess
Note 1: Restrict in your virtual host the usage of .htacess to the folder where you really need it or move the configurations to a virtual host.
Note 2: You probably don't need all those rewrite rules if you have Apache mod_dir installed. Have a look at it
You can also use:
RewriteRule .* - [E=ENV_NAME_HERE:ENV_VALUE_HERE]
I prefer to put SetEnv in the httpd.conf file for the local server, so that all my local ZF apps run in development mode, and when I push to the staging server it has a flag to Set the environment to staging, then the production server has no such setting so it defaults to production...
This way I can keep the same .htaccess file in source control and when I push it out to the different servers it behaves as expected.
middaparka is right about suExec... suExec prevents you from setting custom environment variables in your .htaccess file - according to remi at the WebFaction forum.
If you are running your home server on a Mac or have suEXEC enabled, there are security measures in place to strip any non-http environment variables that are defined, therefore they won't appear in $_ENV['APPLICATION_ENV']. You CAN, however access these through a PHP function to get the same thing.
$var = apache_getenv('APPLICATION_ENV');
You could even define the environment variable within PHP if you want, although not recommended.
$_ENV['APPLICATION_ENV'] = apache_getenv('APPLICATION_ENV');
I have an almost identical config to what you have, and we just define a global:
define('APPLICATION_ENV', apache_getenv('APPLICATION_ENV'));
Once you do that, it should work as desired.

.htaccess not working

I have a .htaccess file to remove the index.php part of the codeigniter-style URL. It's been working fine on one computer but when I copied the file over to my laptop it doesn't seem to do anything. I'm using localhost on both machines. They both run mac osx 10.6 with the bundled apache and php and the latest version of mysql. Everything works fine, it's just the .htaccess that doesn't do what it should. Is there any setting that I might have changed on my first machine and forgotten about?!
EDIT:
I'm wondering if there is something wrong with my CI setup now. If I load the base_url ie http://localhost/~User/project/
then it loads perfectly, adding the index.php.
My config file has
$config['index_page'] = '';
However, as a test I returned this value to 'index.php'. When I loaded the base_url after this it returned: http://localhost/~User/project/index.php/index.php/controller/method
Does this suggest anything to anybody?
SOLVED:
I added a new directory command to my httpd.conf file which targeted the specific site I was working on. Seems to be happy now, but not a very satisfactory way of dealing with the problem if I have several codeigniter sites in my web root.
With just a shot in the dark do you have mod_rewrite installed on both?
If your on Ubuntu, you need to edit /etc/apache2/sites-available/default.
sudo vim /etc/apache2/sites-available/default
Where you see lines that say AllowOverride change it to say:
AllowOverride All
You may also need to enable mod rewrite:
sudo a2enmod rewrite
Then, restart the Apache server
sudo /etc/init.d/apache2 restart
Are you sure you have changed the name of the root folder?
Here is what I do:
I put this on my root folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /rootFolder/index.php/$1 [L]
and in the path you replace "rootFoldel" with the name you are using.
hope that helps!
maybe you changed the path relative to the server root. it can breaks .htaccess and may be solved with a RewriteBase /path/to/folder/ statement
edit
since the problem not seems to be this one above, you could also check the error.log file in the apache folder (probably in the logs folder). it may have your answers and it can tell you if the .htaccess is getting parsed wrong or not parsed at all.

Categories