I have implemented my small website on a live server, however I want to use url-rewrite for some links to make it clean. I try this on my localhost server:
http://localhost/cb/2/login.php
to http://localhost/cb/2/login
I manage to make it work on my localhost server by editing the httpd.conf
of apache and enable url-rewrite. Here's the rule I added to apache:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/cb/2/login$ /cb/2/login.php
</IfModule>
However, when I upload my files to a live server, obviously I do not have privilege to edit httpd.conf
of that server, so I just put my rewrite rule on .htaccess
file and here's the content of it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/login$ /login.php [L,QSA,NC]
But when I try to test it, 404 not found
is the response of server. I also try to implement in on my localhost server assuming I cannot edit the apache config and same error occurs, Object not found!
I have no idea what will I do next. I'm new to this url rewrite rule so any help will be much appreciated :)
UPDATE
After a lengthy discussion with the OP it seems there is a lot more going on behind the scenes.
The end result is no matter what is placed in the .htaccess
file... it doesn't function.
OP was advised to contact the web host, but I will leave my original answer below for others on the site.
Remove the forward slashes from your rewrite rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^cb/2/login$ cb/2/login.php [L,QSA,NC]
You changed your rewrite rule used on localhost when you put it on a live server. Assuming your directory structure is the same you should keep your rewrite rule the same (minus the forward slashes).
Remember in rewrite rules...
1) The text between ^
and $
is what the visitor will input into the url bar, is what your link will be and is what the visitor will see in the url bar.
2) The next block is the location where the actual file exists.
Happy Coding !
UPDATE
If you want your visitor to go to www.examplewebsite.com/login.php
and you want the url to look like www.example.com/login
... your rewrite rule would be simply...
RewriteRule ^login$ login.php [L,QSA,NC] # Handle log-in
Now... let's say that your login.php
is inside another directory called admin
...
RewriteRule ^login$ admin/login.php [L,QSA,NC] # Handle log-in
These two lines mean...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
If the url is not a "real" directory or the url is not a "real" file... then process the rule.
That's why using RewriteRule ^login$ index.php
causes your url to be rewritten as www.examplesite.com/login.php
because the rewrite rule cannot be processed (index.php could not be found), BUT login.php is a "real" file.... so it goes to it.
please try:
Options All -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php