htaccess ignore subdirectory - .htaccess

I have problem with my .htaccess file (or wamp setup).
Here is my .htaccess:
RewriteEngine On
# Options +FollowSymLinks
RewriteBase /
RewriteRule ^directory/?$ directory.php [L,NC]
RewriteRule ^directory/subdirectory/?$ subdirectory.php [L,NC]
When I go to my_web/directory/ - everything is OK, but when I go to my_web/directory/subdirectory/ then show directory.php (url in browser is directory/subdirectory/).
When I write url without rule (example /test/) and I have test.php then automatically show test.php. Why?
Thank you.

Related

Redirect url without changing the url

I know there are a lot of answers available for this but none of them worked for me. I am stuck with this very familiar issue. I have made a website wherein i have to make customised urls for all my franchisees. But all these customised urls should take back to homepage.
Eg. if franchisee enters following url : www.example.com/franchisee/john it should redirect the franchisee to www.example.com but the browser url should remain www.example.com/franchisee/john
I have tried this by modifying .htaccess file but it shows page not found(404) error. Any help will be appreciable. I am new to .htaccess.
Here is .htaccess code :
RewriteCond %{REQUEST_URI} ^.*/franchisee/[a-zA-Z0-9_\.\-]+$
RewriteRule ^(.*)$ / [P]
EDIT :
Here is the complete .htaccess file. This file is present under the docroot folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
Options +FollowSymLinks -MultiViews
RewriteCond %{REQUEST_URI} ^/franchisee/[a-zA-Z0-9_\.\-]+$
RewriteRule ^franchisee/[\w.-]+/?$ / [L]
</IfModule>
You don't need to use proxy or P flag here. Use this rule without R flag to silently rewrite to /:
RewriteEngine On
RewriteRule ^franchisee/[\w.-]+/?$ / [L]

How do I redirect in .htaccess file without changing URL

I want to redirect several pages to a PHP page with GET variables. However, when I try to use an .htaccess file, it redirects but changes the URL. I don't want it to change the URL structure when redirecting. How would I do this?
For example, I want to redirect /fly-snooze-cruise to http://example.com/package_listing.php?package-type=fly-snooze-cruise
and /fly-snooze-cruise/orlando-airport to http://example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport
Here is my .htaccess code:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^fly-snooze-cruise$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise [L]
RewriteRule ^fly-snooze-cruise/orlando-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-airport [L]
RewriteRule ^fly-snooze-cruise/orlando-sanford-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=orlando-sanford-airport [L]
RewriteRule ^fly-snooze-cruise/melbourne-airport$ http://www.example.com/package_listing.php?package-type=fly-snooze-cruise&airport=melbourne-airport [L]
RewriteRule ^snooze-park-cruise$ http://www.example.com/package_listing.php?package-type=snooze-park-cruise [L]

.htaccess RewriteRule slash to get parameter

I just transfered my Domain to a new Server. Mod_Rewrite is enabled on the new server but unfortunately some RewriteRules don't work, while others do. I haven't changed anything in the .htaccess
So the URL www.mydomain.com/go/10.html should make an internal redirect to www.mydomain.com/go.php?name=10
The snippet in the .htaccess looks like this:
# go.php
RewriteRule ^go$ "$0/" [R=301,L,QSA]
RewriteRule ^go/$ go.php [L,QSA]
RewriteRule ^go/.*?([^\.\/]*)\.html$ go.php?name=$1 [L,QSA]
The $_GET["name"] is not available if I call this url.
Replace your .htaccess code with this.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(go)/([^.]+)\.html$ /$1.php?name=$2 [L,QSA,NC]

htaccess rewrite doesn't work properly on localhost

I'm restructuring a site now, and I'm testing it on localhost (xampp).
In the root directory there were a couple of .htm files, which were converted to .php.
However I'd like to keep the .htm extension for SEO reasons.
So I created a .htaccess file with this content:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)\.php$ $1.htm [L]
But when I try to access a page like this to test rewrite:
http://localhost/sitename/anything.php
I always get to
http://localhost/C:/xampp/htdocs/sitename/anything.htm
which obviously produces a 403 error.
I have tried to change .htaccess options, but no success.
Rewrite module is enabled in Apache, so it is not the case.
Am I missing something?
Try adding a RewriteBase directive i.e.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php$ $1.htm [L]
Try to redirect using the requested URI:
RewriteEngine On
RewriteRule ^(.*)\.php$ %{REQUEST_URI}.htm [R=301,L]
I tested it and it added ".htm" to all my php URIs test.php -> test.php.htm
No need for backreferences here.

.htaccess php to html only in root

I been using .htaccess to change php to html and it works great, but I hit a snag when I forgot that some of my static html pages in subfolders don't appear in the web browser.
Is there a way I can make it so I can only have the root folder uses the .htaccess rule and not the subfolders?
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
For this case define your .htaccess rule like this:
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteRule ^(?![^/]+/)(.+)\.html$ $1.php [L]
Negative lookahead will prevent sub directory rule implementation.
You can specific a rule to work only on current dir. Like:
/your/dir/here/.htaccess
You .htaccess will be:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\/here(.*)\.html$ $2.php [L]
$2 mean get second group.
Edit: alternativaly, you can make a .htaccess on subfolder and disable RewriteEngine.

Resources