I have simple php application with navigation based on domain/foo/bar nested urls.
For instance, I have main page index.php with about nav link which should navigate to domain/en/about, where en and about must be transfered to url param like index.php?url=....
But when I click to about I got to domain/en/aboutand
404 not found instead.
I have configured apache2 virtual domain config as:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
<Directory /var/www/html/domain>
Options -Indexes +FollowSymLinks -MultiViews
AllowOverride All
Require all granted
</Directory>
DocumentRoot /var/www/domain/
ServerName domain.local
ServerAlias www.domain.local
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And .htaccess file as:
order deny,allow
RewriteEngine On
RewriteBase /
RewriteRule .* index.php?url=$0 [QSA,L]
mod_rewrite for apache2 is already enabled.
Have no clue what I have missed.
Any help is appreciated!
Thank you in advance!
<Directory /var/www/html/domain>
:
DocumentRoot /var/www/domain/
Your <Directory> section and DocumentRoot directive refer to different locations, so regardless of where you've put the .htaccess file, it's not going to work as intended.
However...
RewriteRule .* index.php?url=$0 [QSA,L]
This rule is not strictly correct, since it ends up rewriting itself on a second pass by the rewrite engine. If it wasn't for the QSA flag, the original url param value (that contains the originally requested URL-path) would be lost. The above ends up rewriting a request for /en/about to index.php?url=index.php&url=en/about. Fortunately, your PHP script still reads $_GET['url'] as en/about. But you can examine the full (erroneous) query string in $_SERVER['QUERY_STRING'].
(And, if you were to simply prefix the substitution string with a slash, ie. a URL-path, you'll get an endless rewrite-loop (500 Internal Server Error). But this could also result from adding additional rules later.)
You should prevent requests to index.php itself being rewritten, which you can do by adding an additional rule. For example:
RewriteRule ^index\.php$ - [L]
RewriteRule .* index.php?url=$0 [QSA,L]
However, this will still rewrite your static assets (assuming you are linking to internal images, CSS and JS files?). So, you would normally need to prevent this with an additional condition that prevents the rule from being processed if the request already maps to a static file.
For example:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?url=$0 [QSA,L]
The CondPattern -f checks if the TestString maps to a file. The ! prefix negates this. So the condition is only successful when the request does not map to a file.
You need parenthesis around what you want to capture. Back-references indices start with '1':
RewriteRule (.*) index.php?url=$1 [L,QSA]
Related
I have two problems actually:
First, I’m trying to redirect several short URLs to a single page with more actions, like this:
RewriteEngine On
RewriteRule ^/login?$ ^login.php?action=login&next=$1 [L]
RewriteRule ^/reset?$ ^login.php?action=reset&next=$1 [L]
This is being written in the .conf file inside <Directory>. The problem is that the first rule gets executed, while the second doesn’t and I can’t figure why.
I also tried writing them like this:
RewriteCond %{REQUEST_URI} /login$
RewriteRule ^login.php?action=login&next=$1 [L]
RewriteCond %{REQUEST_URI} /reset$
RewriteRule ^login.php?action=reset&next=$1 [L]
I should probably mention that login.php does not reside in the root directory, but in different subdirectories.
What am I doing wrong and how can I fix it?
The second issue I have is that if I put an .htaccess file inside the root directory, the rules in the .conf file don’t get executed anymore.
Inside the .conf file I have these rules:
<Directory>
Options Indexes FollowSymLinks ExecCGI Includes MultiViews
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
</Directory>
Why is this and how can I fix it?
Just to extend on from my comments above. Place these rules in your site root .htaccess or in httpd.conf file:
Options -MultiViews
RewriteEngine On
RewriteRule ^/?login(?:/(.*))?$ subdir1/login.php?action=login&next=$1 [L,NC,QSA]
RewriteRule ^/?reset(?:/(.*))?$ subdir1/login.php?action=reset&next=$1 [L,NC,QSA]
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
Version Information: Apache v2.4.18 on Ubuntu, PHP version 7.1,Symfony v3.2
I've been trying to get this working for a couple of days now and keep hitting problems, I have an existing PHP application using a custom built framework with the following Apache VirtualHost configuration:
<VirtualHost *:80>
ServerName dvlp.mydomain.com
DocumentRoot /var/www/my-site/com
RewriteEngine On
# Do nothing for the Home page ('^/$'), specific directories, static files.
RewriteRule ^/(?:$|shared|asset)|\.(?:php|ico|txt|xml) - [L]
# Search engine friendly request URIs (most, but not all, without a query string).
RewriteRule ^/([a-z0-9-]+/?)$ /index.php?param1=$1 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-\.]+/?)$ /index.php?param1=$1¶m2=$2 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-]+)/(.+)$ /index.php?param1=$1¶m2=$2¶m3=$3 [QSA]
<Directory />
Options FollowSymLinks
AllowOverride None
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>
What I am trying to do is install a Symfony application to work within a sub-directory. I've created my Symfony application in /var/www/symfony-app and created a symbolic link /var/www/my-site/com/my-symfony which points to /var/www/symfony-app/web.
I then tried the following on line 6 of my VirtualHost file:
RewriteRule ^/(?:$|my-symfony|shared|asset)|\.(?:php|ico|txt|xml) - [L]
Visiting dvlp.mydomain.com/my-symfony in the browser takes me to the directory listings for the /var/www/symfony-app/web folder. I tried adding an index.php file to /var/www/symfony-app/web which simply includes app_dev.php and it loaded the home page, but I I tried visiting dvlp.mydomain.com/my-symfony/edit-personal-details I get the 404 error page from my existing application.
I realised now that I need separate rewrite rules for this sub-directory, for Symfony this is usually written as the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
So I undid the change I made to line 6 of the VirtualHost file and added a new line as follows:
RewriteRule ^/my-symfony(.*)$ /my-symfony/index.php [L,QSA]
This results in a 404 error:
No route found for "GET /my-symfony/" (from "http://dvlp.mydomain.com/")
And if I try visiting dvlp.mydomain.com/my-symfony/edit-personal-details I get the same 404 error:
No route found for "GET /my-symfony/edit-personal-details" (from "http://dvlp.mydomain.com/")
The last thing I have tried is adding a prefix to my Symfony routing.yml:
my_symfony:
resource: "#MySymfonyBundle/Controller/"
type: annotation
prefix: /my-symfony
This then loads the pages correctly but with no images or stylesheets because they are trying to load from the DocumentRoot (e.g dvlp.mydomain.com/css instead of dvlp.mydomain.com/my-symfony/css).
I would appreciate any input as to where I am going wrong. Thanks.
I finally managed to get this working as intended by adding a "Location" tag containing specific RewriteRules and a RewriteBase for the Symbolic link. For information, final VirtualHost config below:
<VirtualHost *:80>
ServerName dvlp.my-domain.com
DocumentRoot /var/www/my-site/com
RewriteEngine On
<Location /my-symfony>
RewriteBase /my-symfony
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
</Location>
# Do nothing for the Home page ('^/$'), specific directories, static files.
RewriteRule ^/(?:$|my-symfony|shared|asset)|\.(?:php|ico|txt|xml) - [L]
# Search engine friendly request URIs (most, but not all, without a query string).
RewriteRule ^/([a-z0-9-]+/?)$ /index.php?param1=$1 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-\.]+/?)$ /index.php?param1=$1¶m2=$2 [L,QSA]
RewriteRule ^/([a-z0-9-]+)/([a-z0-9-]+)/(.+)$ /index.php?param1=$1¶m2=$2¶m3=$3 [QSA]
<Directory />
Options FollowSymLinks
AllowOverride None
ErrorDocument 404 /404.php
</Directory>
</VirtualHost>
I installed PHP and Apache server in my computer.
So inside of "htdocs" I created 2 files(index.php, Contact.php) and a directory(MyClass), after that inside of "MyClass" I created a file(class.php)..
In web browser when I am using the url "http://localhost/MyClass/class.php", the result is : "class.php" sending data to the web browser.
In the same situation is there any way in PHP/Apache to take control of it from the "index.php" ??
Or
I want to be known about all the requests inside of "index.php" which came from web browser, is it possible ????
But I don't want to use any GET variable like "http://localhost/index.php?class=page2"..
Apology for my bad English.
Thanks..
You should use include, in your case you would use
include 'MyClass/class.php';
More information about include can be found right here
I'm not sure I understand correctly but a way to not use ?class=page2
is to create a .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will rewrite all requests to non existing files or folders to your index.php
the use $_SERVER['REQUEST_URI'] to make your navigation.
for example you could use http://localhost/class/page/2
$_SERVER['REQUEST_URI'] would then be class/page/2
If your website is in a subfolder of htdocs be sure to edit
RewriteBase /dir/here/
[...]
RewriteRule . /dir/here/index.php [L]
to match it
My problem solved.
Which changes I did they are below :
In "C:\Apache24\conf" need to change file "httpd.conf"
Just active :
1)
LoadModule rewrite_module modules/mod_rewrite.so
2)
<Directory />
#AllowOverride none
AllowOverride All
Require all denied
</Directory>
3) I am using "Virtual Host", so in "C:\Apache24\conf\extra" need to change file "httpd-vhosts.conf"
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#test.com
DocumentRoot "E:/TEST"
<Directory "E:/TEST">
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
ServerName test.com
ServerAlias www.test.com
ErrorLog "logs/test.com-error.log"
CustomLog "logs/test.com-access.log" common
</VirtualHost>
If you are not using "Virtual Host", then I think you need to add some lines to the "Directory" inside of "httpd.conf" !!
<Directory>
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
Use the PHP include function. You can include your MyClass/class.php in you index file. You can then add an htaccess file to restrict files in the MyClass directory from being viewed directly.
I have a subdomain set up on my hosting: indiantimes.indianradio.net.au, that is being pulled from a folder in my /public_html folder: /public_html/indiantimes.com.au.
I am trying to write an .htaccess rule that will redirect it to that folder still, but retain the original url the user typed in: indiantimes.indianradio.net.au.
I have only been able to get the redirect working, i.e. (indiantimes.indianradio.net.au redirects to indianradio.net.au/indiantimes.com.au/), but I can't seem to get the redirect working so the url seen by the user, stays at: indiantimes.indianradio.net.au. The majority of the image urls are broken intil I am able to get the redirect working properly.
The .htaccess rule I was playing around with was:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.indiantimes\.indianradio\.net\.au$
RewriteRule ^/?$ "http\:\/\/indianradio\.net\.au\/public_html\/indiantimes\.com\.au" [R=301,L]
What am I doing wrong with the redirect? Any help would be much appreciated! Thanks in advance!
You have to replace your sub folder name to be the same as your sub domain
(indiantimes.com.au -> indiantimes).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^indiantimes\.indianradio\.net\.au$
RewriteCond %{REQUEST_URI} !^/indiantimes/
RewriteRule (.*) /indiantimes/$1
source
for subdomains it is generally recommended to add a virtualhost in apache instead of using .htaccess (preformance-wise and more cross-platform).
However you might find the following link suitable in case editing the apache config files isn't an option: .htaccess rewrite subdomain to directory (summary: using mod-proxy and adding the P flag to your RewriteRule)
Go to /etc/apache2/sites-available (use cd in terminal)
Add a new file named: indiantimes.indianradio.net.au, example content:
<VirtualHost *>
DocumentRoot /var/www/indianradio.net.au/public_html/indiantimes.com.au/
ServerName indiantimes.indianradio.net.au
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews +Includes
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error-logfile.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access-logfile.log combined
</VirtualHost>
link to the file in apache2/sites-enabled
in terminal: ln -s ./indiantimes.indianradio.net.au ../sites-enabled/ from the sites-available folder, note the trailing /!
I am working with a custom MVC PHP framework and the index page (acting as a router) receives a GET variable "do" which contains the path that it will route to. If this variable is not set, it defaults to the Auth controller, method login.
require_once('config.php');
$controllerAction = isset($_GET['do'])?$_GET['do']:"auth/login";
require_once('core/main.php');
Then the index page (source code above) passes this $controllerAction to the main.php file, which autoloads the main controller and then loads the requested controller.
Thus, the URIs in this framework are of the form mysite.com/?do=controller/method/variable and I need it to be in the form mysite.com/controller/method/variable.
Here is the .htaccess file I tried to use, it just didn't work (I have other htaccess files working on the same server so it's not an Apache problem) :(
RewriteEngine On
RewriteRule ^([^/]*)$ /?do=$1 [L]
Someone suggested that I can do this using PHP but I am not sure how to go about that.
Edit:
The error is that I get "This page cannot be displayed", 404 errors, whenever I try to directly access the mysite.com/controller/method links rather than the default mysite.com?do=controller/method
Further Edit
(please note that other virtual hosts work fine on my localhost):
(XAMPP) Apache Virtual Hosting Info:
<VirtualHost *:80>
DocumentRoot "D:\sites\mysite.com\root\wwwroot"
ServerName mysite.com
ServerAlias mysite.com
<Directory "D:\sites\mysite.com\root\wwwroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
File structure (Windows):
D:\
--sites
----mysite.com
--------#client_details
--------root
-----------#devfiles
-----------#vars_pwd
-----------wwwroot
--------------config
--------------core
--------------application
------------------controllers
------------------libraries
------------------models
------------------views
----------------------css
----------------------javascript
----------------------images
----------------------icons
First of all, there are some issues with your .htaccess contents. It's always a good idea to not rewrite if a file with the requested name exists. This allows you to have an img/ folder for your images or any other static content like css files, javascript, downloads, etc.. The first RewriteCond tells Apache to only rewrite if no folder with this name exists. The second one does the same with files. Then you probably want the QSA (i.e. Query String Append) option, which will pass all other GET variables to your script.
Under this conditions you can simplify the regex and use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L,QSA]
You might be surprised because this is more or less the same as others posted. I use similar things for many of my projects and I've just tested it, I can guarantee that it works. There must be something wrong with your apache config.
When you have problems with mod_rewrite, the first thing you should try is to enable the module itself. Type these commands as root in your shell:
a2enmod rewrite
/etc/init.d/apache2 restart
The first one activates the module (or complains with Module rewrite already enabled if everything is ok) and the second one restarts your Apache server. The path may of course be different on your server.
Then you have to make sure that your VHost config allows you to use .htaccess files and do rewrites. This means AllowOverride must be set to at least FileInfo (or All). You could also try to put the rewrite rules right into the config file. Your config should look similar to this:
<VirtualHost *:*>
ServerName test.example.com
ServerAlias www.test.example.com
DocumentRoot /home/sites/test/
<Directory "/home/sites/test/">
Allow from all
AllowOverride All
Options +Indexes
</Directory>
</VirtualHost>
Note that you have to restart Apache if you change anything in there.
If that all doesn't help, it's always a good idea to have a look at the error logs. On my system they're located at /var/log/apache2/error.log (debian). They might give you more information on what's going wrong.
Try
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?do=$1 [L]
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L]
Check your apache logs, access logs specifically. If the folder is present in the web root, then you should be able to access it directly :). You might also want to check if you have duplicate virtualhost entries for the same site by chance.
This one is my customized MVC framework which is based on cake
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]
</IfModule>
May be this should help. The typical URL pattern for this site.com/controller/method
I don't know what your domain setup is like, but here are some suggestions.
If your code resides in the root of your folder, and the index file is called index.php try the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?do=$1 [L,QSA]
If your website exists in a subfolder e.g. www.example.com/site/, and the index file is index.php Then try the following (change /site/ to whatever your folder is).
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?do=$1 [L,QSA]
If you still get the 404 error message then do the following:
Make sure your site allows .htaccess files to be processed by checking AllowOverride is set to all. If you don't have access to the necessary config files to check, a simple test is to setup an .htaccess rule to redirect to a dummy file on your system. If it works, then your .htaccess is being executed fine.
Have a look at your MVC framework to see what page it's actually sending the request to. The problem may be that you haven't defined a handler for that particular request, and the default action of your MVC framework is to throw a 404 error.
Edit: Just reading your description, I notice you said that the URL should basically be something like mysite.com/?do=controller/method/variable. If it has be very strict about this format, then you'll also need to put in rules for removing any leading or trailing slashes, e.g. the following re-write rule should do it:
RewriteRule ^\?(.*)\?$ /index.php?do=$1 [L,QSA]
(This makes the leading and trailing slashes optional, but it should remove them from the actual value you pass to do).