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.
Related
I feel like this is a rather common request, but I am too confused about .htaccess and couldn't find a solution by Google.
I have a Laravel instance in a subdirectory of the Apache2 htdocs. Now I would like to invisibly redirect all requests from the root domain to this folder (it should be the "root" website). But the tricky thing is, this is not the only folder, there are other folders directly in the htdocs, which should be reached normally. Just the "root" website is not in the root but also in a subfolder. For example:
https://domainA.com should load https://domainA.com/laravel/public (including possible query string or parameters, but invisibly for the user)
https://domainA.com/websiteB should be served as it is
https://domainA.com/websiteC should be served as it is
...
I assume, part of this solution will be to list all the websiteB, websiteC directories in the .htaccess, would it be possible to automate this?
Thanks in advance!
You can put a .htaccess in the folder you want to custom controle but you have to create some filter condition
<IfModule mod_rewrite.c>
RewriteEngine On
## RewriteBase /foo
## conditions to tell what to redirect ie on URI
## RewriteCond %{REQUEST_URI} ^/a-folder/
## not websiteB or websiteC
RewriteCond %{REQUEST_URI} !^/websiteB/
RewriteCond %{REQUEST_URI} !^/websiteC/
## if the file does not exist call index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ my/path/to/a/script.php [L]
</IfModule>
After you have to do something special in script.php for those HTTP calls
You can also rewrite the URI and pass it again to apache but things can be complicated after...
I have a custom web app and the file structure works like this:
/apps/calendar/frontend/index.php
/apps/calendar/frontend/view/index.php
/apps/calendar/backend/index.php
/apps/calendar/backend/edit/index.php
/apps/calendar/backend/add/index.php
/apps/calendar/backend/view/index.php
I'm trying to write a .htaccess file to help redirect the files so they cant see the 'real' path.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^([^/\.]+)/(.*)/(.*)($|/$) /apps/$1/frontend/$2/$3 [NC,L]
RewriteRule ^([^/\.]+)/(.*)($|/$) /apps/$1/frontend/$2 [NC,L]
RewriteRule ^([^/\.]+)($|/$) /apps/$1/frontend/ [NC,L]
When I visit localhost/calendar it should map redirect to /apps/calendar/frontend/index.php. But when I visit localhost/calendar/add it gives me a 301 (permanent move) then shows the full page of localhost/apps/calendar/frontend/add/index.php in the console. Anyone got any ideas why this would happen? Or a better way around this? The apps might have heaps of sub-directories so, I'm not particularly keen on having a rule for ever subdirectory combination.
As you can see also I have a /admin path, which would load the /backend/ parts of the app. I would assuming I can do the similar code with the prefix of /admin?
This question might also be of your interest: Create blog post links similar to a folder structure.
Given that your .htaccess is located on the root folder of your domain /home/youraccount/public_html/.htaccess, it would look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(admin|apps) [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^([^/]+)/?(|.*)$ /apps/$1/frontend/$2 [NC,L]
RewriteCond %{REQUEST_URI} !^/apps [NC]
RewriteCond %{DOCUMENT_ROOT}/apps/$1 -d
RewriteRule ^admin/([^/]+)/?(|.*)$ /apps/$1/backend/$2 [NC,L]
Let's say the user access:
http://domain.com/calendar
http://domain.com/calendar/
http://domain.com/calendar/add
All the above would redirect to
/apps/calendar/frontend/index.php
/apps/calendar/frontend/index.php/
/apps/calendar/frontend/index.php/add
And the if the user access:
http://domain.com/calendar/admin
http://domain.com/calendar/admin/
http://domain.com/calendar/admin/add
It would go to:
/apps/calendar/backend/index.php
/apps/calendar/backend/index.php/
/apps/calendar/backend/index.php/add
So it would make index.php your controller for each end:
/apps/calendar/frontend/index.php
/apps/calendar/backend/index.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)
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]
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