URL Rewrite not working on localhost in ubuntu - php

So i'm running lampp on ubuntu 11.10 and i have the following problem
i need this url:
localhost/myname/oefeningenPHP/cms/index.php?head=about
rewritten to:
localhost/myname/oefeningenPHP/cms/about
The url is kinda messy because i have lots of folder with different PHP exercises for school.
My .htacces is located in:
localhost/myname/oefeningenPHP/cms
since cms is my sites root.
.htacces contains the following code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/([a-z]+) /index.php?head=$1
</IfModule>
But returns a 404 error.
Furthermore i'm sure the rewrite module is being loaded since it's uncommented in the http.conf file
I've ran out of ideas so i'm hoping someone here can help me. I'm not very sure about the forward slashes in the RewriteRule, but i've tried various things and nothing works

try
RewriteRule ^([a-z]+) index.php?head=$1 [L]
You should also see if you .htaccess is enabled by adding some junk to it e.g. asdfasdfasdfasd, save it and try to access the URL. If you do not get a 500 error, then it is not enabled.
To enable it look for a line that says
AllowOverride None
and change it to
AllowOverride All

If you have already run sudo a2enmod rewrite command but your URL Rewrite is not working.
To make .htaccess files work as expected, you need to edit this file:
Step1 : sudo vi /etc/apache2/sites-available/default
Step2 : Look for a section that looks like this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
You need to modify the line containing AllowOverride None to read AllowOverride All.
step 3 : sudo /etc/init.d/apache2 reload

Related

How can i block direct access to specific folder?

There is my problem, i have an img tag, src gets image from icon/ folder and i need to disable direct link access to that folder and content inside that when user type something like 'mywebsite.com/icon/logo.png', using htaccess, but allow content inside src attribute to get and display it without 403 error message 'access denied' in browser console, i made lot of research but nothing helped me, if i missing something please provide your opinions thanks.
my .htaccess code is here:
<IfModule mod_rewrite.c>
<If "%{HTTP_HOST} == 'http://mywebsite.com/icon/'">
deny from all
</If>
<Else>
allow from all
</Else>
</IfModule>
The best way to do this is disable it with webserver apache2. In my 14.X Ubuntu - open /etc/apache2/apache2.conf you have to change from
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
to
<Directory /var/www/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
now just restart apache.
to restart apache2 run:
sudo service apache2 restart
This will disable directory listing from all folder that apache2 serves.

Virtual Host not parsing PHP

Alright, I have spent hours searching through very similar questions but I still don't seem to have found the answer:
I have set up a virtual host with the following in my httpd-vhosts.conf
Listen 80
<VirtualHost *:80>
ServerName test.dev
ServerAlias test.dev
DocumentRoot "/Users/OrangeSoda/OneDrive/Projects/Test/Website"
ErrorLog "/private/var/log/apache2/test.dev-error_log"
CustomLog "/private/var/log/apache2/test.dev-access_log" common
<Directory "/Users/OrangeSoda/OneDrive/Projects/Test/Website">
Options all
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
In my http.conf I have Include /private/etc/apache2/extra/httpd-vhosts.conf as well as LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so uncommented.
When I go to test.dev/index.php which just has <?php echo "PHP TEST"; ?>, I see the contents of the file output as a string.
It seems as though PHP is not enabled and is therefore not being recognized.
If i go to localhost, I do see the "It Works!" page, so I know php is working, just for some reason not on my virtual host.
Someone suggested to put php_admin_flag engine on but that just makes the whole page crash.
Does anybody understand why php is not enabled here?
Have you checked if port 80 is opened? It might help more if you include detailed log.
You're on the right track but you need to actually load php onto the virtual host. On the <Directory> entry, add these lines :
<IfModule sapi_apache2.c>
php_admin_flag engine on
</IfModule>
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
I got the solution from here.
It looks like mod-php may not be installed. What OS are you on?
On Ubuntu/debian:
sudo apt-get install php5
on redhat:
sudo yum install php
The solution was that while php was enabled, for some reason the default setting in httpd.conf was not to read .php files as .php.
Although I am not really sure why that would not be the default setting, I added the following to my httpd.conf and now it works just fine:
AddType application/x-httpd-php .php

how to config htaccess file after enabling mod_rewrite module in ubuntu 14.04?

I already install apache2 then i enable the mod_rewrite module like this:
sudo a2enmod rewrite
after enabling i am confused about these lines, i can't understand what to do with them, as far as i know that what i have to do with these lines is that " find the following section, and change the line that says AllowOverride from None to All. "
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
my problem is that I can't find a file with the above lines to edit.
I already looked in /etc/apache2/sites-available/. In that directory, I only found two files:
000-default.conf
default-ssl.conf
In both files i can't find above lines which i have to edit.
i edit the file /etc/apache2/apache2.conf in this directory but it still didn't worked at all.
i am working in /var/www/html/project_name directory then why it's giving me 403 forbidden error.
when i open info.php in this /var/www/html/info.php it show php information and works fine. whenever i switch to my project directory it's gives me 403 forbidden error
Still .htaccess is not running. It's giving me 403 forbidden error.

how do I enable execution of php scripts in the per-user public_html directories

I have a php script to upload files in a directory.
This works fine in apache's DocumentRoot.
When I copy the code in my /home/$USER/public_html , instead of the right output I just when a printout of the php file. I presume it does not get executed.
I was looking into the userDir module and found this
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
includesNoExec looked promising but it is related to cgi only.
So how can I enable php scripts to be executed for every user?
Try setting this configuration:
<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_value engine On
</Directory>
</IfModule>
Don't forget to restart Apache.
service apache2 restart
It turns out the important bit of code that Claudix provided is already present in the configuration files under /etc/apache2/mods-available/php5.conf
the clause there contains php_admin_value engine Off , so by setting the Claudix solution anywhere will conflict with this setting. What is more, the comment above suggests NOT to set php_admin_value engine On as it renders .htaccess files unable to disable php.
As such the correct way to run php in user www directories is by commenting out the relevant text in /etc/apache2/mods-available/php5.conf
I assume you are using apache2 on debian like I do and I think the solution
for you is to add Alias dir and set that alias dir config like below
1 edit the apache2 configuration from terminal run this command
sudo gedit /etc/apache2/sites-enabled/000-default
2 edit the path you will to execute PHP files and save
Alias /testdir /home/user/testdir
<Directory /home/user/testdir>
Options FollowSymLinks
DirectoryIndex index.php
AllowOverride All
</Directory>
3 restart apache2
sudo service apache2 restart
check If you can access the folder from browser if not probably you will have to chmod/chown the folder
yourhost/testdir
According to Apache 2.2 documentation this can be done by uncommenting this configuration line:
#Include conf/extra/httpd-userdir.conf
and modifying httpd-userdir.conf to suit your server. Based on your qustion text, this should resolve your issue.

Laravel redirects to a route but then apache gives 404 error

I have a site that is working on the same server in a different url (staging), but now I've deployed the site and the base url ("/") is redirected to the login url (so laravel is sort of working), but then I get a 404 error from apache.
If I use sub.domain.com/index.php/route, it works, but if I use sub.domain.com/route redirects to the login route and gives a 404 error.
I also changed the routes.php to return the login view in the route "/" and it show the login form correctly.
The problem might come from a module in your Apache server called rewrite module.
in windows you can just uncomment this line from your httpd.conf
#LoadModule rewrite_module modules/mod_rewrite.so
I'm running Ubuntu 14.04 and enabled it using
sudo a2enmod rewrite
Try these with an Apache restart.
It might work for you as well.
After adding
AllowOverride All
to the vhost configuration, got it working. Probably the default configuration wasn't allowing the redirects?
Here's my final (and working) vhost configuration:
DocumentRoot /var/www/sitefolder/public
ServerName site.domain.com
<Directory /var/www/sitefolder/public>
AllowOverride All
allow from all
Options +Indexes
</Directory>
For VirtualHost
Only add these lines into httpd.conf of your apache:
<Directory /var/www/sitefolder/public>
AllowOverride All
allow from all
Options +Indexes
</Directory>
Or you can replace the first line with:
<Directory />
And if all doesn't work, you can try:
<Directory />
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Categories