My question is quite simple. I have my main page (www.domain.com/index.php)
Is there a way to redirect the user to /index.php when he only types www.domain.com?
And also remove the index.php from the url when he's on it?
I checked a bit about .htaccess, but none of the tricks seem to do the job.
Thank you!
EDIT
Here's my .htaccess:
# To set your custom php.ini, add the following line to this file:
# suphp_configpath /home/yourusername/path/to/php.ini
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
<Files .htaccess>
order allow,deny
deny from all
</Files>
Options All -Indexes
As you can see, I put DirectoryIndex index.php in it, but doesn't change it :(
Simply put
DirectoryIndex index.php
in your .htaccess
When the user types www.domain.com it'll take index.php as default index page and won't be display in the address
in the Domain control panel mention Start / Index page as index.php.
Usually index.html/htm is the default page.
You will find these option under site proerties.
Redirect / http:/your.domain.php/index.php
RewriteEngine On
RewriteBase / # URL path corresponding to this directory
RewriteRule (^|.*/)index\.php$ $1 [R]
Related
I am trying to redirect my project folders one level up in stucture.
I would like to change URL adresses as follows:
http://localhost/my-website/projects/project1/
http://localhost/my-website/projects/project2/
...
to
http://localhost/my-website/project1/
http://localhost/my-website/project2/
...
In root folder I do not have these project folders, they are in "projects" folder
the structure is like this
/root/
/projects/
/project1/
index.php
/project2/
index.php
/project3/
index.php
index.php
.htaccess
So if user enters URL http://localhost/my-website/project1/, the URL stays the same but it works as if he entered the full adress. Is this achievable? I tried so many options so far and I am still not able to do it...
First of all, don't use a .htaccess file, if you can modify the Apache config files.
It's better to add these types of configuration inside the vhost config instead.
With that said:
.htaccess
<Location "/">
AllowOverride None
Options FollowSymLinks
Require all granted
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^project(.*) projects/project$1 [QSA,L]
# RewriteRule ^project([0-9*]?)/(.*) projects/project$1/$2 [QSA,L]
</IfModule>
</Location>
/my-website/project1/ becomes /my-website/projects/project1/
/my-website/project1/whatever becomes /my-website/projects/project1/whatever
Uncomment the one that is more up your ally,
the first one is more generic and catches all after project,
the second one is more specific and looks for a number.
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.
I have XAMPP installed, I run everything on my localhost/
So imagine I have localhost/website/index.php or www.website.com/index.php;
How can I delete index.php and/or replace it with something like;
localhost/website/:) or www.website.com/:)
or show it like;
localhost/website or www.website.com
Example;
$URLcapture = $_SERVER['PHP_SELF'];
$URLcustom = ":)";
$_SERVER['PHP_SELF'] = $URLcustom;
echo $_SERVER['PHP_SELF'];
This shows :) of course but how can I get :) in my URL?
Thank you,
F4LLCON
EDIT
Under loaded modules I can see mod_rewrite.
STEPS BEFORE EVERYTHING -- START -
In httpd.conf I've deleted;
# from infront of LoadModule rewrite_module modules/mod_rewrite.
Changed;
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
and
<Directory "C:/xampp/cgi-bin">
AllowOverride All
Options +FollowSymLinks
Order allow,deny
Allow from all
</Directory>
STEPS BEFORE EVERYTHING -- END -
In .htaccess I've added
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
and also tried
RewriteEngine On
RewriteBase /
RewriteRule index.php nieuw.php
What am I doing wrong?
EDIT 2
Not touching .htaccess at all worked.
1. Closed xampp and apache.
Edited httpd.conf in C:\xampp\apache\conf;
2a Start with above steps;
2a STEPS BEFORE EVERYTHING -- START - till
2a STEPS BEFORE EVERYTHING -- END -
2b. added at the end of http.conf;
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/home$ /spiderweb/Web-projects/Web-stage/Nieuwidw/index.php
RewriteRule ^/link$ /spiderweb/Web-projects/Web-stage/Nieuwidw/link.php
RewriteRule ^/contact$ /spiderweb/Web-projects/Web-stage/Nieuwidw/contact.html
RewriteRule ^/about$ /spiderweb/Web-projects/Web-stage/Nieuwidw/about.html
</IfModule>
start xampp/apache and type localhost/home
3a. the website will open but the layout is not there. So it does not load style.css
3b. .css is in;
3b. C:\xampp\htdocs\spiderweb\Web-projects\Web-stage\Nieuwidw\includes
3c. images are in;
3c. C:\xampp\htdocs\spiderweb\Web-projects\Web-stage\Nieuwidw\includes\images
Next
on home you can see "more" button under every product, if you press on more you will be redirected to the products page in link.php but every product gets it's own ID in the URL;
4a. http://localhost/link.php?id=126 so the page shows up; not found because the URL is different
How to fix these problems?
In php :
header("Location: http://www.example.com/");
But the best way is using htaccess (like ManseUK says)
If you are able to the best way of doing this is using mod_rewrite - you then include a rule like the one below and the index.php is hidden
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I have an issue with htaccess that is blocking php script access.
this is the error message I receive when i load up the page:
You don't have permission to access /index.php on this server.
When i remove the .htaccess file i can access index.php with no issues.
The same .htaccess file was working fine on a different hosting that i use.
My .htaccess file (EDIT: rearranged after suggestion by toopay)
RewriteEngine On
<Files .*>
Order allow,deny
Allow from all
</Files>
Options FollowSymLinks
RewriteRule ^photos.+$ thumbs.php [L,QSA]
RewriteRule ^[a-zA-Z0-9\-_]*$ index.php [L,QSA]
RewriteRule ^[a-zA-Z0-9\-_]+\.html$ index.php [L,QSA]
I did try to set the file priviledges to 644, 755, 777 and still not working with any settings.
Could you please help me and see what i have wrong, as this is the first time it is happening and the same .htaccess file is working fine on a different domain (folder) on the same hosting.
Couldn't there be problem with <Files .*>? I think this is a wildcard pattern so you should use just <Files *>.
I managed to find the solution, the issue was really simple, i was missing a + before FollowSymLinks. Simple issue, few wasted hours to find the solution. The weirdest part is that the on the same hosting, different domain/folder it is working the same without the +... Anyway thank you for your help.
this is my final working code:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^photos.+$ thumbs.php [L,QSA]
RewriteRule ^[a-zA-Z0-9\-_]*$ index.php [L,QSA]
RewriteRule ^[a-zA-Z0-9\-_]+\.html$ index.php [L,QSA]
The order of your htaccess, should be...
RewriteEngine On
<Files .*>
Order allow,deny
Allow from all
</Files>
Options FollowSymLinks
RewriteRule ^photos.+$ thumbs.php [L,QSA]
RewriteRule ^[a-zA-Z0-9\-_]*$ index.php [L,QSA]
RewriteRule ^[a-zA-Z0-9\-_]+\.html$ index.php [L,QSA]
I'm looking to use .htaccess to redirect to one page or another based on if a file exists in the directory.
Basically I need all visitors to index.php to be sent to either home.php page if splash.php does not exist or splash.php page if splash.php does exist in the directory, so far i have this...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} index.php
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^.*$ /splash.php [L]
This sends visitors to mysite.com/splash.php if it exists only if I specifically have mysite.com/index.php as my URL, it doesn't work if just use the mysite.com URL.
I understand the first 3 lines of my code, but not the !splash.php -f bit (i got that from some other redirect code i found on SO).
How do i redirect to home.php if splash.php not present?
Cheers!
Try this rule. It should work on mysite.com/index.php, mysite.com/ or mysite.com
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^(index\.php|/|)$ /splash.php [L]
You'll need to break it into multiple blocks, so try this (goes to splash.php if it exists, regardless of whether or not they have index.php in the file name, and then if splash doesn't exist goes to home.php):
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/splash.php -f
RewriteCond %{SCRIPT_FILENAME} !splash.php
RewriteRule ^.*$ /splash.php [L]
RewriteCond %{DOCUMENT_ROOT}/splash.php !-f
RewriteRule ^.*$ /home.php [L]
mate, I added a quick .htaccess and it works perfectly for me. I'm not sure what might be different in your configuration though. To avoid any confusion, here's the Apache (2) vhost that I'm using:
<VirtualHost *:80>
DocumentRoot "/usr/local/zend/apache2/htdocs/test-setup"
ServerName test-setup
ErrorLog /var/log/apache2/lps-version-one_test-setup_error_log
LogLevel warn
CustomLog /var/log/apache2/lps-version-one_test-setup_access_log combined
<Directory "/usr/local/zend/apache2/htdocs/test-setup">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
When I have both files in the directory, I'm redirected to splash.php (even though the name of the file in the url doesn't change - internally I'm redirected to splash.php). When I rename splash.php (or remove it) I only see index.php. When I put it back, the redirection works as normal. When I specifically put index.php in the url, with both files available, I'm redirected to splash.php.
Fwiw, one of the best things to remember about mod_rewrite is this:
Despite the tons of examples and docs,
mod_rewrite is voodoo. Damned cool
voodoo, but still voodoo.
-- Brian Moore bem#news.cmc.net