Automatically rewriting URLs using .htaccess? - php

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]

Related

.htaccess URL rewrite rules folder

I have this in my html folder
--html
--.htaccess
--app
--application_name
--public
--index.php
--landing_page
--index.php
As of now, everytime the website url is accessed (www.website.com), the browser opens the landing_page/index.php.
How can I make it that when I access www.website.com/beta, I will be doing a request to html/app/application_name/public/index.php
I have tried adding this to the .htaccess
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
This is the whole .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
But it does not work. Not sure what term to use to search for this kind of problem.
Your root is currently set at /html, and therefore the RewriteBase / is at /, the /beta/ simply does not exist. You could change the RewriteBase /app/application_home/public/ and set the RewriteRule ^beta/?$ index.php [L,NC]. This however no longer give you the access to the landing page, not sure if this is what you want?

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]

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

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.

Sending specific folder to HTTPS and the rest from non-www to www, while keeping the rest of the laravel routes working?

I am trying to write a .htacess file such that:
The site runs php 5.4
Requests to domain.com run index.php first
Requests to http://domain.com/checkout are redirected to https://domain.com/checkout
All requests to domain.com are redirected to www.domain.com
So here is my attempt:
<IfModule mod_rewrite.c>
# Use PHP 5.4
AddType application/x-httpd-php54 .php
Options -MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/checkout|/order)
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>
But when I make a post to domain.com/cart the user is automatically redirected to domain.com/index.php
Please let me know where I am going wrong...
First off, you need to understand that such rules in .htaccess files are run on a first-come, first-serve basis. Also, you are using the L flag for each of them.
So, you should make sure that the two conditions and rule for silent-mapping to index.php should come last. Move those down to below the https and www-on rules.
Now, of course a request to /cart will be mapped to index.php; that's what it's programmed to do. However, you say "redirected"... Does that mean that it shows index.php in the address bar? If that is the case, the fix I've mentioned should sort that out (always check for redirects first, and then do the necessary mapping).
Like #scotsninja said in the comments, these things should be handled by Laravel itself. Your .htaccess file should only be used to map anything that is not a file or directory to the index file, or Laravel bootstrap.

Directory access

I'm writing web application using codeigniter framework. Here is the structure of document root
application
system
website
What I want is that only the website directory to be accessаble. Here is the content of .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule .* /website/%1 [QSA]
When I type the site url in browser I'm redirected to website dir. So far, so good. It works.
The problem comes when I try to access a dir within the website directory. I get
404 page not found
What to do so the directories below the website dir to be accessаble ?
This is the rule you need:
RewriteRule (?!^website(/.*|)$)^.*$ website%{REQUEST_URI} [L,NC]
Try
RewriteEngine on
RewriteBase /
RewriteCond $1 !^website
RewriteRule ^(.*)$ website/$1 [L]

Categories