PHP -- Using .htacess rewrite rules to hide folder name in URL - php

I'm doing a small php personal project for fun and I need help with my rewrite rules. My goal is to hide completely a folder name in my URL.
Here's what I got already :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/my-app/
RewriteRule ^(.*)$ /my-app/$1 [L]
</IfModule>
This is working fine, I don't need to specify my-app folder in the URL but when I redirect to another page the folder name is displayed again. What can I do to specify that when my-app folder is requested, to hide it from the URL. Thanks in advance.

Try adding this rule to your htaccess file:
RewriteCond %{THE_REQUEST} \ /+my-app/
RewriteRule ^my-app/(.*)$ /$1 [L,R=301]
This will redirect any direct request for anything in /my-app/ to a URL with it removed.

Related

Automatically rewriting URLs using .htaccess?

I'm trying to automatically rewrite a URL to a virtual directory using .htaccess.
My rules are working but not automatically, so for example I have a file called login.php in the root directory and I want to rewrite to to a virtual directory to be like this example.com/login/ instead of example.com/login.php, the below rule works fine for this purpose.
RewriteOptions inherit
RewriteEngine On
RewriteBase /
RewriteRule ^login/$ ./login.php [L,NC]
However, this won't rewrite the login.php file automatically. I need whenever a user requests the login.php file, the rule automatically redirects the user to example.com/login/ and still serve the same content. How could this be achieved?
You will need to a redirect for that:
ewriteOptions inherit
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /login\.php[\s?] [NC]
RewriteRule ^ /login/ [R=301,L]
RewriteRule login/$ ./login.php [L,NC]

Redirect folder access to file (but not redirect files in folder)

I currently have a webpage set up at abc.com/folder/ and there are multiple .php files in this folder that need to be accessed. Because of conflicts between a smooth scroll script and my shared navigation files, I am looking for a .htaccess rule that will redirect abc.com/folder/ to abc.com/folder/index.php, but will not redirect abc.com/folder/xyz.php. Currently I have:
RewriteEngine On
RewriteRule ^/?folder/ ^/folder/index.php [R=301,L]
but this redirects all files to index.php, while I just want the base directory redirected but none of its files. does not change anything.
Edit: Currently I have this:
RewriteEngine On
RewriteRule ^/?folder/ ^/folder/index.php [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
and it doesn't do anything at all.
You can use this simple rule in root .htaccess:
RedirectMatch 301 ^/folder/?$ /folder/index.php
This will redirect to /folder/index.php when URI is /folder/
The following conditions will tell Apache to not rewrite when the requested file exists.
RewriteCond %{REQUEST_FILENAME} !-f
This is useful if you have other types of rewrite rules, or you send everything to a single file.
Just off the top of my head, but how about
RewriteRule ^/?tradeshow/$ ^/tradeshow/index.php [R=301,L]

how to redirect the user if there are any directory name in the url htaccess

hello all i am working on a site where i dont want any user to browse my site directory or to view any image/file inside the directory by just typing the address of the image or file in address bar or by any software help.
i think this is possible my .htaccess rewrite rule
can anyone help me please
my site directory names are members,images,ads
from some where on this site i found this
RewriteCond %{REQUEST_URI} ^/news/latestnews/(.*)$
RewriteCond %$1 ^(.*)2012(.*)$
RewriteRule ^(.*)$ /news/2012 [R=301,L]
which redirects the user to new directory but i dont know how to work with htaccess i can not modify the code
You can add following to top of the htaccess file you already have
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?YOURDOMAINNAME.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.YOURDOMAINNAME.com [L]
Options -Indexes will restrict directory listing
RewriteCond %{HTTP_REFERER} !^http://(www\.)?YOURDOMAINNAME.*$ [NC] checks if the request to a file is from within script or if the request has a referrer from your domain
RewriteRule \.(gif|jpg|png)$ http://www.YOURDOMAINNAME.com [L] will redirect to your home page, if some one tries to access the file directly. You can added file extension to this rule by separating it by pipe | symbol, e.g. gif|jpg|png|doc|pdf

htaccess file not working for wordpress pages

I'm trying to add a new rule in the htaccess file that will allow input of other instead of the actual name of the page which is notify. But I can't seem to get it to work.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^other$ notify [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Also tried something like:
RewriteRule ^other$ index.php/notify [QSA]
and this:
RewriteRule ^other$ /index.php/notify [QSA]
Also tried adding the rules from wordpress itself (inside the admin_init section of a plugin) and then refresh the rules by going to the permalinks settings, choosing post name and then save but no luck either. Upon checking htaccess file it produced a rule similar to the above one:
add_rewrite_rule('other', 'index.php/notify', 'bottom');
I've verified that I can override the rules via the htaccess file in the root directory because when I tried to use an actual file in the server hello.php
RewriteRule ^other$ hello.php [QSA]
It works. I just don't know why when I use a wordpress page it doesn't work. It also seems to work when I use the absolute path:
RewriteRule ^other$ http://somesite.com/notify [QSA]
But this is no good because when I add some arguments to the URL (which is what I will ultimately do) I end up with bad looking URL when I do the above: http://somesite.com/?some_argument=some_value
The current permalink setting in wordpress is set to post name. Any ideas what I have missed?
Try this rule:
RewriteRule ^other/?$ index.php?p=PAGE_ID [QSA]
Put the necessary PAGE_ID to redirect to
Hello may be you can use .htaccess plugin..! to control it from admin panel. I hope this Help you
http://wordpress.org/plugins/wp-htaccess-control/

kohana htaccess and index structure

I am using kohana 3.2
My site is www.mysite.com/best, kohana is installed on best.
I am using the default htaccess file with rewritebase /best
so what is the best way to redirect users that go to www.mysite.com?
Right now if someone puts www.mysite.com/helo (www.mysite.com/best/helo)
It loads but apache gives 404 not found.
I hope my question makes sense.
EDIT
If a user goes to main domain (www.mysite.com) I want it to load kohana located at mysite/best.
but if a user right now types the full thing and omits best, site will load but my forms wont work because it is posting to www.mysite.com/helo not www.mysite.com/best/helo
instead of loading the fake helo controller, it must show a page not found.
EDIT
Ok I added this to htaccess file and it works perfectly
RewriteCond %{REQUEST_URI} !^/best/
RewriteRule ^(.*)$ /best/$1
You can't have multiple rewritebase in htaccess so...
rewrite your rules to fit you need in /best
or put an other htaccess in / to rewrite your needs.
Try this .htaccess instead:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /best/
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
#RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
RewriteRule .* index.php?/$0 [PT,L,QSA]
Note that the trailing / on RewriteBase is required.

Categories