In order to convert my dynamic URL i.e www.3idiots.co.in/index.php to static url i.e www.3idiots.co.in/index.html, I edited my .htccess file and put the following code in it:
RewriteEngine On
RewriteRule ^index.php$ /index.html [R]
when i uploaded this file in the root directory,and try to open the page, I got the error
404 page not found error, www.3idiots.co.in/index.html not found.
You have to actually have a file named index.html. Right now you don't. The rewriting/redirecting is working fine, you're just redirecting to a non-existent page/file.
I'm a little confused as to what you're actually trying to do. If you just want to move index.php to index.html, rename the file. Rewriting makes it so that if someone tries to open index.php they will be redirected to index.html, but you still have to have an index.html file for them to be redirected to.
RewriteEngine On
# Send the user to index.html
RewriteRule ^index.php$ /index.html [R]
# Tell the server that index.html really means index.php
RewriteRule ^index.html$ /index.php
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /index\.php
RewriteRule ^index\.php$ /index.html [L,R=301]
RewriteRule ^index\.html$ index.php [L]
The first rule redirects every direct request of /index.php externally to /index.html. And the second rule rewrites requests of /index.html internally to /index.php.
Are you sure mod rewrite is enabled & working?
1) create an html page called found.html with whatever you want in it, but some text to be sure it's loaded (not a blank page basically) and put this in an file called ".htaccess" :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ /found.html [L]
2) upload both your .htaccess and the found.html files in your domain's root
3) Just try to load -www.example.com/find.html (with your real domain of course). If mod_rewrite is available, you should see the content of found.html while browsing find.html (which does not physically exist by the way).
If it does not work, try :
RewriteEngine on
RewriteBase /
RewriteRule ^find.html$ found.html [L]
In the Apache Conf file, you also need to make sure that AllowOverride is set to a value that will allow .htaccess to be processed.
Normally it's AllowOverride all
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^$index.htm/$ index.php [L]
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index.html$ index.php
Try this code...
It will work for your problem /..
Best Of LUck
If it solve your problem..
Visit my site
Related
So, I've this problem:
Base Website located at http://example.com/
Second Website located at http://example.com/web2/
People making various requests to the second website like
http://example.com/myWeb/pg1 and http://example.com/web2/pg2
Recently and due to some other issues I need to have a custom new path for the second website but also keep the first one working.
The ideia is to allow users to access the second website over the two following addresses:
http://example.com/web2/
http://example.com/alternative-url-web2/
The folder /web2/ actually exists on the server, but how can I simulate the folder /alternative-url-web2/ and "redirect" the requests to /web2/?
Please note I don't want the URL on the browser to change, this must be a "silent redirect". And I also make sure that all other requests like http://example.com/other are not redirected by the second website.
Thank you.
Update:
According to #anubhava I could simply solve this issue by adding in my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
This is probably working fine but I noticed the following:
http://ex.com/alternative-url-web2 is redirected to http://ex.com/web2/ (changing browser URL);
http://ex.com/alternative-url-web2/ is redirected to http://ex.com/(changing browser URL);
http://ex.com/alternative-url-web2/someRequest works fine and does NOT change the browser URL;
http://ex.com/alternative-url-web2/index.php works fine and does NOT change the browser URL;
Site Note:
At /web2/ there's an .htaccess that might be cause the wired redirect behavior above... So here is the file contents:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Can the internal RewriteRule to index.php be causing all this? If yes, how can I fix it?
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^alternative-url-web2(/.*|)$ /web2$1 [L,NC]
Alternate code:
RewriteRule ^alternative-url-web2/?$ /web2/ [L,NC]
RewriteRule ^alternative-url-web2/(.+)$ /web2/$1 [L,NC]
This is a pretty simple rewrite. In the htaccess file in your document root, just add the following:
RewriteEngine On
RewriteRule ^alternative-url-web2/?(.*)$ /web2/$1 [L]
Unlike a redirect, which makes the browser/client send a new request for a new URL (thus changing what's in the browser's location bar), a rewrite happens entirely on the server's side.
By the way, in order to follow the trail of htaccess redirects, you could add something like this to each of them:
Header add X-Remark-Rewrite "/path.to/htaccess"
You can inspect these in the response in the developer tools.
I have a proxy I've been writing with PHP for a while.
Right now, requesting a url like www.mysite.com/proxy/?folder/page.html on my server will return the page at www.theirsite.com/folder/page.html.
I basically am having my index.php file use everything past /proxy/? as the request URI for www.theirsite.com. Any images are copied to the folder /proxy/images/ and the src attributes of the <img> tags are changed accordingly. All this is working great.
Now I would like to change my script so that I will not need the ? anymore. However, the url www.mysite.com/proxy/folder/page.html would result in an HTTP request to page.html, which doesn't exist on my server.
This isn't what I want. I need index.php to be loaded instead, so it can return the page at www.theirsite.com/folder/page.html. To accomplish this, I imagine I would need to use Apache's mod_rewrite, which is working with my WordPress installation.
What would I need in my .htaccess file to do this correctly, while still allowing access to files that exist in the /proxy/images/ directory? Would this affect $_SERVER['REQUEST_URI'] at all?
RewriteEngine on
RewriteRule ^/$ /index.php [R]
Well, I got it working how I wanted. Here's the full contents of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /proxy/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy/index.php [L]
</IfModule>
Now for the url www.mysite.com/proxy/folder/page.html, index.php is loaded, and $_SERVER['REQUEST_URI'] returns /proxy/folder/page.html. Files that actually exist in the /proxy/ folder or any subfolders do not rewrite to index.php, which is what I wanted.
how can I show folder name instead of file name?
For example,
www.example.com/home/index.php -> www.example.com/home
I know when I browse www.example.com/home I will get the result but that is not what I want because it will add a / behind the folder name (e.g www.example.com/home/).
What I want is without the / behind the folder and when user browse www.example.com/home/index.php the page will redirect the user to page not found. The purpose I do this is to hide the language I used and make the link more readable and memorable.
I found something like rewrite the rules in .htaccess file but I am new in php so I don't how to make it. Anyone can give me suggestion or provides some tutorial about this.
Thanks.
To remove the slash, you must first disable DirectorySlash
DirectorySlash Off
DirectoryIndex disabled
Direct access to PHP files can be answered with a R=404 status code
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
And then, you can rewrite requests pointing to a directory and containing an index.php
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^.*$ /$0/index.php [L]
This RewriteCond looks, if the requested URL is a directory and if there is an index.php in this directory. If this is the case, then the index.php is executed.
Putting all together
DirectorySlash Off
DirectoryIndex disabled
RewriteEngine on
# prevent direct access to PHP files
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
# rewrite requests for a directory to index.php
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^.*$ /$0/index.php [L]
Htaccess doesn't have anything common with PHP. Htaccess is Apache's configuration file, so you need to play with htaccess to achieve that what you want. On the other hand your solution will be really dirty. Learn about MVC and FrontController to make your structure cleaner.
In my site root directory. there are donghua.php and index.php. now, is there a way to use .htaccess file to let the visitor access my site .the default shows example.com/donghua.php. not example.com/index.php. thank you. the server is Apache.
ps:The user still can access example.com/index.php
i using DirectoryIndex donghua.php in the .htaccess. the default page is ok. but when i access example.com/index.php it redirect to example.com/donghua.php. if i forbid it redirect how do i do?
If you have access to the virtual host config file (or you should even be able to do this in htaccess) you can set the DirectoryIndex to be donghua.php if you want, i.e. instead of something like this:
DirectoryIndex index.html index.php default.html
Just do:
DirectoryIndex donghua.php
So whenever someone goes to your website, example.com, your apache installation will read donghua.php as the default index file.
Change your redirection rule in .htaccess file for index.php. If you open the .htaccess file, you can find the follwing lines,
RewriteCond %{THE_REQUEST} ^.*/example.php
RewriteRule ^(.*)example.php$ http://www.test.com/$1 [R=301,L]
Change or remove this rule for index.php. Better remove or comment it then it won't redirect to another page
"RewriteCond" this code check the codition if the browser requesting the file name index.php. If the condition is satisfy, then the next line will execute that is "RewriteRule". This "RewriteRule" will redirect your URL.
Search and find the name "index.php" in .htaccess file. comment out that lines. syntax to comment is "#".
Example:
#RewriteCond
#RewriteRule
Take back up before changing anything in .htaccess file.
To make your URL lovely and suitable for search engine ranking.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]
To redirect
redirect 301 /old-page.php http://www.yoursite.com/new-page.php
What rule should i set, to make the mod_rewrite ignore the directory "public" completely?
By that, I mean, the files should be accessible within it, but if the file does not exist, a server error page should come up with something like, FORBIDDEN, or FILE NOT FOUND what ever. I do not need custom error pages or stuff like that. I simply want the "public" to behave like there is no mod_rewrite at all.
Here is my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
My file structure is
/system/
/application/
/public/
I want the folder public to behave, like there are no rewrite rules set at all, completely ignore it.
edit
That's my .htaccess file:
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I already had this .htaccess in the /public/ folder:
RewriteEngine off
I've tried all the different answers above (and a ton from google). I've tried to mix 'em up what so ever.
My folders:
/system/
/application/
/public/
/public/.htaccess #RewriteEngine off
/public/favicon.ico
/index.php
Below are the url with results I'm getting:
/public/favicon.ico -> I get the favicon
/public/faviDon.ico -> I get the index.php (without mod rewrite you would get "not found")
/public/ -> I get the index.php (without mod rewrite "forbidden")
So it still does rewrite urls, if the file was not found, or upon accessing a folder directly.
Can you se it?
Thank you very much for effort guys! I really appreciate it!
EDIT
I completely setup your files on my machine
// /.htaccess
RewriteEngine on
RewriteRule ^(public)($|/) - [L,NC]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
.htaccess in the public folder:
// /public/.htaccess
Options -Indexes
RewriteEngine off
This disables rewriting like you wanted.
/public/ -> 403 Forbidden
/public/favicon.ico -> 200 File found
/public/not-existing.ext -> 404 File not found
Do you have a index.php in you public folder?
Maybe you could remove that one..
What kind of machine your testing on?
I tested it on Linux + Apache 2 + PHP5.3
I can give you more support in the afternoon (my time +2 GMT)
EDIT 2
When I remove this line from /.htaccess is still works
RewriteRule ^(public)($|/) - [L,NC]
Everything is handled by the .htaccess in the public folder.
Maybe it's a caching problem in your browser. Try a different browser/clean up history/install app to remove cache.. (depending on what browser you're using)