Is there a way to get this by .htaccess file? - php

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

Related

Redirect subdirectory to root subdirectory without changing url PHP

https://example.com/app1
This is my subdirectory and it has one index.html file running on apache server.
I need to redirect all the requests to this directory like https://example.com/app1/* to https://example.com/app1 without changing the URL.
If user access to https://example.com/app1/test it should be redirected to https://example.com/app1 but URL should remain same as
https://example.com/app1/test
You can use this rule in your public_html/. htaccess :
RewriteEngine on
RewriteRule ^app1/((?!index\.html).+)$ /app1/ [NC,L]
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]*)$ index.php [nc,qsa]
This one worked for me.
Thanks to all.

HTACCESS: URI rewrite everything to index.php (Within the same directory)

I have a uri: www.domain.com/leasing/properties/
Within that directory, I created a file .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ index.php?uri=$1 [L,QSA]
What I want, is that regardless of that users type after properties/ (Ej. www.domain.com/leasing/properties/somethigelse/I-do-not.care/what##$%they\type) to be handled by www.domain.com/leasing/properties/index.php
I do not need structure or anything else. index.php will grab $_REQUEST['uri'] and check against a database. So, again, I do not care what's typed after www.domain.com/leasing/properties/ so long index.php can get the URI and handle an action according to $_REQUEST['uri']
I want the address bar to not not change. Whatever a user types, to remain the same.
The current file is giving me a 404 error.
PS. I know close to nothing about Apache .htaccess
Create a .htaccess file with the following:
RewriteEngine On
RewriteRule ^([^/]+)(.*)$ /leasing/properties/ [R]
And place it within /leasing/properties/ folder.
RewriteEngine on
RewriteRule ^/leasing/properties/(.*)$ /leasing/properties/index.php [L,QSA]
If that does not work, your Apache server may not be configured to allow .htaccess files.

URL Rewrite/Mod Rewrite .htaccess on Apache and PHP

I have a single point of entry on my website
mysite.com/admin/index.php?View=List&Model=User
All access to the DB/application is through this index.php file.
I would like to clean my URL from what is shown above to.
mysite.com/admin/List/User
The idea is to remove the key 'Model and 'View'.
I have found this link that looks very similar.
PHP/Apache: Rewrite rules with .htaccess
in htaccess file insert this code :
RewriteEngine On
RewriteRule ^admin/([^/]*)/([^/]*)$ /admin/index.php?View=$1&Model=$2 [L]
The rewritten URL:
http://mysite.com/admin/List/User
this site very useful for generate rewrite URL
http://www.generateit.net/mod-rewrite/
Create a file named .htaccess in your web root if you haven't already got one with the following:
# Turn the rewrite engine on (skip this if already done)
RewriteEngine On
# Redirect admin/List/User/ to admin/index.php?View=List&Model=User
RewriteRule ^admin/([^/.]+)/([^/.]+)/?$ admin/index.php?View=$1&Model=$2 [L]
This might work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^admin/([^/]+)/([^/]+) /index.php?View=$1&Model=$2 [NC]
Add to .htaccess in the root folder of your website.

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

URL rewriting-page not found error

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

Categories