Changing URL PHP jQuery or mod_rewrite - php

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]

Related

Rewrite URL one folder up

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.

PHP SEO friendly with clean URL

Currently I works on to transfer my site into SEO friendly URL (in localhost),
Here's the original URL with query string:
http://{ip}/sitename/item.php?category=44
I want convert to:
http://{ip}/sitename/item/category/44
.htaccess file (same directory with item.php):
DirectoryIndex index.php
Options -Indexes
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ item.php%{REQUEST_URI} [L]
RewriteRule ^/item/([0-9]+) /item.php?category=$1
</IfModule>
<Files *htaccess>
Deny from all
</Files>
in item.php, I use $_GET['category']
$category = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_GET['category']));
$list = $listing->get_items($category, $mysqli);
if($list == 0){
echo '<p><h2>Page not found</h2></p>';
die();
}
Problem 1:
When I loaded http://{ip}/sitename/item/category/44, the page cannot get the variable passes to get_item(), the page is shown Page not found? 44 is suppose return a value.
Problem 2:
My page doesn't loaded referrer files, all *.css *.js etc are just ignore?
Try this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule item/category/(.*) item.php?category=$1
PROBLEM 2 - The problem has arised as you have not mentioned the base path.
You need to assign base path to load css and other references.
Try using this in <head> tag <base href="http://www.MYSITE.com/" />
This will load your css/js.
Problem 1: No rewriting has happened here – (you only think it has, beause your script item.php got called anyway, because you had item in the URL and MultiViews has done its work) – paths the RewriteRules match on in .htaccess never start with a /, so remove it.
Problem 2: Has been discussed many times before (and should also be obvious to anyone who knows the basics of how completion of relative URLs works) – see f.e. .htaccess URL Rewrite Problem (Scripts don't load)
RewriteEngine On
RewriteBase /sitename
RewriteRule ^item/([0-9]+)/?$ item.php?category=$1 [L,NC]

.htaccess redirect if file exists

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

Website redirect

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]

CodeIgniter configuration with mod_rewrite, xampp and windows

I'm a noob to CodeIgniter and am trying to figure out the configuration for an app I'm building. Something is wrong with my setup.
I'm running XAMPP on Windows and am using an alias directory to point to the applications directory. In other words: "http://localhost/app_name/ " points to the root directory of the application. It all seems to work well until I do the .htaccess for mod_rewrite. Then every time I try to go to a controller I get pitched back to the xampp root.
My config is:
Directories
/app_root
/app_root/codeigniter // where code igniter is located.
/app_root/main // where the main app is located. It' the applications
// directory cut from code igniter and renamed.
.htaccess
<IfModule mod_rewrite.**so**>
RewriteEngine On
RewriteBase **/app_name/**
RewriteCond %{REQUEST_URI} ^codeigniter.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
index.php
$system_folder = "#codeigniter";
$application_folder = "main";
app_name/main/config/config.php
$config['base_url'] = "http://localhost/app_name/";
$config['index_page'] = "";
app_name/main/config/routes.php
$route['default_controller'] = "welcome";
I should also state that the app_name directory is an alias for a different drive than the apache root.
Apache Root: c:\xampp\htdocs\
App_name: d:\projects\app_name\development\
The alias is:
Alias /app_name "d:/projects/app name/development"
<Directory "d:/projects/app name/development">
Options Indexes FollowSymLinks ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Thanks in advance for the help... And if you don't mind please "explain" what you're doing when you answer with code. I want to know what I'm doing wrong. If you can help me with this I'll buy you a beer (via PayPal). This is frustrating.
Success!!
I finally managed to get URL rewrite working and what a long arduous journey it was. Here is what I got working finally. Take note that there is no backslash on the RewriteBase. Very interesting given what I've read. Thanks to everybody who tried to help.
# Options
Options -Multiviews
Options +FollowSymLinks
#Enable mod rewrite
RewriteEngine On
#the location of the root of your site
#if writing for subdirectories, you would enter /subdirectory
RewriteBase /app_name
#Removes access to CodeIgniter system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css
#folders, and the robots.txt file
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
If your using XAMPP on a local machine, your should use internal rather than mod_rewrite.
It will load your pages under the alias name.
it took me a while to figure that out - apparently you should use mod_rewrite on remote servers to achieve the same thing.
RewriteBase /
in your .htaccess should be
RewriteBase /app_name/
to specify which directory it is..
First, a question. Is your $system_folder variable really set to:
$system_folder = "#codeigniter";
or was that a nerf from the weird (to me) way SO uses markdown? If it is, remove the #. It is an invalid character for directory/file names.
Next, I believe your RewriteBase should be /, since you use an alias in Apache, but don't quote me on that.
I personally use the .htaccess format supplied here: CodeIgniter URLs in the User Guide; under the heading Removing the index.php file. There are many ways to do it, however. A quick Google search yields a couple thousand.
Do you have mod_rewrite enabled? Check the forum post here.

Categories