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...
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.
The next block is the location where the actual file exists.
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
Related
Background
I'm creating a time tracking app with PHP on my localhost (MAMP). The app structure is as follows
htdocs/time-tracker/public/index.php
Issue
No matter how many configurations I try, I can't seem to avoid some sort of weird glitch with the URL.
What I need
I want the following result. When I visit the url 127.0.0.1:8888/time-tracker on my local machine, I trigger the php app, routing all requests through the htdocs/time-tracker/public/index.php. Preferably without a trailing slash, but priority is just to get the app to work.
My current .htaccess file
RewriteEngine On
RewriteBase /time-tracker/
RewriteRule ^public\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public [L]
Updates
1. $_GET parameters change outcome
For some reason http://127.0.0.1:8888/time-tracker?debug=true and http://127.0.0.1:8888/time-tracker get me different results.
http://127.0.0.1:8888/time-tracker?debug=true results in a redirect to http://127.0.0.1:8888/public
http://127.0.0.1:8888/time-tracker results in a redirect to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
Neither of these results are what I want.
2. Partially working
This .htaccess file has gotten my redirects to work whenever I put something in the $_GET
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php [L]
For example, 127.0.0.1:8888/time-tracker/?test=test works while 127.0.0.1:8888/time-tracker/ still redirects to http://127.0.0.1:8888/Users/etc/etc/htdocs/time-tracker/public
3. Not redirecting properly on root
The redirects works on all paths except for the root path. For example, 127.0.0.1:8888/time-tracker/test and 127.0.0.1:8888/time-tracker/?test=test both work, just not 127.0.0.1:8888/time-tracker/
I don't know why my regex won't pick this up. Here is my code
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* public/index.php [L]
4. Seeing an empty path name
I've tracked it down to one last issue: empty paths don't register with the redirect.
# Settings
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine On
# Rules
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*|x) index.php?/var=$1 [L]
For some reason, it just can't catch the redirect if the path is empty.
5. Close enough solution
This is the best I got. This is actually working, but I couldn't fix the trailing slash issue.
# Settings
DirectorySlash On
RewriteEngine On
# Rewrite
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?/var=$1 [L]
Hopefully somebody can come solve the trailing slash directory root issue or at least confirm that it is impossible. At this point, the correct answer goes to anyone who can explain my mistakes and make this into a helpful post.
Try this right after RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
Try this. Put your .htaccess file in the time-tracker folder
RewriteOptions inherit
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /time-tracker/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
So having very little experience with Regex as well as minor beef with .htaccess I need to ask this:
I want to use AJAX on my site, but my mod rewrite configuration prevents me from doing this properly, as I redirected all urls to my index.php and set the url $_GET variable, this is my current setup:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Now I want this rule to not fire if the query contains "&ajax&" as a string, at least I always append "&ajax&time=..." to the ajax path that I want to load.
So I thought it would be easiest to check if I can match "ajax" and use the [S]kip or [L]ast Flag to prevent the other rule to fire, but haven't been able to achieve it yet, any help? With explanations or pointers toward some?
Also, it would be neat if this doesn't require absolute urls or anything, as I want it to work in both development (localhost) and live (actual domain) environment without changing the .htaccess file accordingly.
SO
I want the url example.com/profile
to be redirected to example.com/index.php?url=profile
AND
I want the url example.com/src/file.php?stuff=something&ajax&time=1234
to stay example.com/src/file.php?stuff=something&ajax&time=1234
BUT
I want the url example.com/ajax
to also be redirected to example.com/index.php?url=ajax (just in case)
Any help is greatly appreciated, thanks!
You just need to add another RewriteCond in your rule that skips this rule if query string contains ajax:
Options -Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)ajax [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
The htaccess file shouldn't rewrite requests for valid files. This is the line that says so:
RewriteCond %{REQUEST_FILENAME} !-f
So what you want should already be happening, assuming src/file.php exists on the server.
I uploaded the code to the server. It started the homepage correctly . But when I press any link , I get 404 not found error. I discovered that I need to add index.php to my url for it to work.
so it will be like that:
mydomain.somee.com/myWebsite/index.php/anotherPage
When I was working locally using Xamp as a server, I didn't get any of those problems.
I got those problems after I uploaded the website to some.com which apparently doesn't use .htaccess file (editing or removing has no effect).
How to add this index.php automatically and hide it from the user?
I didn't change any of the system files or the htaccess
please tell me if you need anymore files or description.
You need to redirect all your all pages through index.php file but remove it from URL.
Write below rules in your root .htaccess file:-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
OR
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
To understand, How htaccess rules are working, This link will help you :)
Hope it will help you :)
I want all the request to be rewrited to index.php so I used this htaccess code.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
It works fine when site is hosted on home directory. But suppose if I move my site to a subdirectory xyz and move all my files including .htacess file inside this directory. Then if I access http://example.com/xyz/some_page, the request is not redirected to /xyz/index.php.
So, How can I make this rewrite work even on subdirectories as in my case.
Update:
I forgot to mention the directory xyz as in my case is likely to change frequently. So, this directory doesn't need to be hard coded in the rewrite rule
Remove the RewriteBase
If you don't need the rewrite base - just remove it. A working example based on the one shown in the question would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Note that the first rewrite rule has been deleted as it is logically equivalent to RewriteCond %{REQUEST_FILENAME} !-f (do not rewrite requests for a file that exists).
Consider also deleting RewriteCond %{REQUEST_FILENAME} !-d as this prevents urls like /this/folder/exists being sent to index.php. Most relevant if Directory listings are enabled but not desired.
change your base url for rewrite
RewriteBase subdir/
or try with full url
RewriteBase http://www.examle.com/subdir/
I have a real weird issue
I'm playing around with .htaccess and trying to redirect all requests to the /test/ folder's index file.
My site lies in a folder /test/ in my local htdocs folder. No other files exist currenlty.
What I expect:
When I visit any url, (for example /test/category/one/) I should be redirected to /test/index.php
What happens
I get a 404 Not Found
My .htaccess looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?__route=$1 [L,QSA]
I have tried setting RewriteBase /test/
This is as straight forward as it gets so why isn't it working?
I have a Wordpress site in another folder and that works flawlessly with custom rewrites.
I even copied the Wordpress' .htaccess contents to the test site's, substituting the rewrite base and last rule with /test/.
Wordpress' .htaccess: (which works on a seperate WP install on same server)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>
# END WordPress
I have been struggling with this for a while now and read quite a few SO articles with no help.
I even write a rewrite log file and now there shows nothing when I browse to the test site but a visit to the Wordpress site writes quite a few lines.
I am running XAMPP on a Win64 machine.
Any help would be greatly appreciated! =)
Update: Also, make sure the line endings in your .htaccess file are set appropriately. Apache can sometimes choke on anything that doesn't include the new-line (\n) character.
So it looks to me like you want to (at some level) emulate what WordPress is doing. Here's how I handled this case when I was developing some software that did the same thing:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.php [L]
</IfModule>
For files that exist (i.e. either the -f or -d test passes), we serve them up unchanged. Otherwise, we redirect incoming requests to index.php. Note that the /test portion of the path is not included in the RewriteRule, since the RewriteBase set up where we were starting from. So, in your example, I think it would end up being:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ /index.php?__route=$1 [L,QSA]
</IfModule>
FWIW, I'm no .htaccess expert. I've simply found this to work for me in the past.
Also, if you're on a shared host (like DreamHost), you may need to set up the appropriate rules for allowing default error documents. Some shared web hosts serve up a single file (failed_auth.html is one example) for error cases. If you're not filtering out that case, you may end up with a 404.
This should do the trick:
# Activate the rewrite module.
RewriteEngine On
# Ensure the requested URL is not a file.
RewriteCond %{REQUEST_FILENAME} !-f
# Ensure the requested URL is not a directory.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?__route=$1 [L,QSA]