I am not very familiar with .htaccess, but have the following in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^centurion/programme-(.*).php$ ./centurion/programme.php?cid=$1
RewriteRule ^klerksdorp/programme-(.*).php$ ./klerksdorp/programme.php?cid=$1
RewriteRule ^tygervalley/programme-(.*).php$ ./tygervalley/programme.php?cid=$1
RewriteRule ^rustenburg/programme-(.*).php$ ./rustenburg/programme.php?cid=$1
RewriteRule ^witbank/programme-(.*).php$ ./witbank/programme.php?cid=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The code above basically make the url's "friendly" by changing the url's to something more descriptive for dynamic pages and removing the .php extentions of the "static" pages.
This all works perfect. But now I need to redirect error/file not found pages as well. I tried adding the following (with relative paths and absolute paths):
ErrorDocument 404 /index.php
But when I enter an incorrect url (in the browser), I still get "file not found" error (not the redirected page).
When I add the following, all the pages redirect to the index.php page:
RewriteRule ^.*$ /index.php [L]
When I have only the following code in my .htaccess file, the incorrect url's redirect as expected.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ /index.php [L]
But then, obviously, non of my "friendly" url rules work.
How can I have both the "friendly" url rules as well as the incorrect url redirects work together?
Related
How can I do the following on the localhost which will be hosted later online:
I have this link:
http://localhost/shops/shop.php?c=15
I want to rewrite it to http://localhost/shops/shop/15
htaccess is(which is placed in C:\wamp\www\shops folder):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?RECIPES/(.*?)/?$ /single-product-details.php?=$1 [L]
RewriteRule ^/?SHOP/(.*?)/?$ /shop.php?=$1 [L]
With your shown samples, please try following Rules. Place your htaccess file along with shops folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /shops/
##External redirect to friendly url.
RewriteCond %{THE_REQUEST} \s/shops/(shop)\.php\?c=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite to actual url/files.
RewriteRule ^([^/]*)/(.*)/? $1.php?c=$2 [QSA,L]
I have a small site that contains two pages, the homepage (index.php) and a booking (booking.php) page which I would like to hide their extensions.
I am using the following code in my .htaccess file but it doesn't seem to work as I'd like.
What I am trying to do is simply make booking.php appear as example.com/booking and my index.php always use example.com.
RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z0-9_-\s]+)/?$ /$1.php [NC,L]
Redirect /booking.php /booking
Redirect /index.php /
I would redirect all non-asset related requests to index.php using something like the following for your .htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [qsa]
Then inside your index.php file, do something like:
if ($_SERVER["REQUEST_URI"] == "/")
{
//index page code
} elseif ($_SERVER["REQUEST_URI"] == "/booking")
{
//booking page code
} else
{
header("HTTP/1.0 404 Not Found");
}
This way it's relatively easy to add additional pages without touching your .htaccess and you can keep any included files etc out of your webroot.
The lines "RewriteCond %{REQUEST_FILENAME} !-d" and "RewriteCond %{REQUEST_FILENAME} !-f" check to see if a requested file exists e.g. an image
before sending the request to index.php which is effectively a fallback.
You are mixing mod-alias (Redirect directive) with mod-rewrite (RewriteRule directive) . These are two different apache modules with different runtime behaviour. You can use the following mod-rewrite rules :
RewriteEngine on
RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# redirect /index.php and /booking.php to their extension - less versions
RewriteCond %{THE_REQUEST} /(index|booking)\.php [NC]
RewriteRule ^.+$ /%1 [L,R]
#rewrite /index and /booking to original destination
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z0-9_-\s]+)/?$ /$1.php [NC,L]
I've installed Drupal 7 to a sub-directory on my server and used .htaccess mod_rewrites to fix most of the URL pathing issues, I'm now looking for a way to strip the sub-directory from the URL when it's explicitly requested.
To be clear, the site can be accessed from 'example.com' and the internal links work fine, but I'm looking to prevent requests to 'example.com/sub/file' from working, Ideally with a 301 back to 'example.com/file'.
My .htaccess at the moment looks like this:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ sub/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f
RewriteRule .* sub/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* sub/index.php?Q$0 [QSA]
Have you tried adding:
Redirect 301 /\/sub/file http://www.example.com/file
to your .htaccess?
I have a webpage where if non-existing files or folders were to be typed by a user http://wwww.somewebsite.com/nonexistingfolder or http://www.somewebsite.com/nonexistingfile.html will just simply forward to www.somewebsite.com. Then would like to create a web page from www.somewebsite.com/about.html to www.somewebsite.com/about since shorter is better in my opinion. Using .htaccess I thought I could use RewriteCond for non-existings and RewriteRule for the user-friendly like web page url. I'm terrible at .htaccess I only know basics, I did my research even the questions that may have been asked already but not sure how to write this exception rule.
How can I add a code in .htaccess so that I can have all non-existing files/folders except the web page url I specified?. This one below will simply redirect all non-existings to index.html and when I do www.somewebsite.com/about (from /about.html) simply goes to index.html. Any help?
--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
# Rewrite existing files sample RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/$ index.html [L]
RewriteRule ^about/$ about.html [L]
RewriteRule ^services/$ services.html [L]
RewriteRule ^application/$ application.html [L]
RewriteRule ^contact/$ contact.html [L]
RewriteRule ^privacy/$ privacy.html [L]
You need to do the all-or-nothing rewrite (e.g. the RewriteRule ^(.*)$ / [L,QSA]) after all of your other more specific rules. The rules all get applied in the order that they appear in, so this means your first rule simply always gets applied, and then the rewrite engine stops.
Swap the order around and try again:
# Rewrite existing files sample RewriteRule ^contact/$ /pages/contact.htm [L]
RewriteEngine on
RewriteRule ^/?$ index.html [L]
RewriteRule ^about/$ about.html [L]
RewriteRule ^services/$ services.html [L]
RewriteRule ^application/$ application.html [L]
RewriteRule ^contact/$ contact.html [L]
RewriteRule ^privacy/$ privacy.html [L]
--- my .htaccess shows --
# Redirect non-existing files or folders to index
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
Also this rule:
RewriteRule ^/$ index.html [L]
Will never get applied because leading slashes are removed from the URI before applying rules to them, so ^/$ will never match, you want ^$ or ^/?$ instead.
I am using a CMS called Vivvo, and vivvo comes with a .htaccess file from default with the following rules:
RewriteEngine On
RewriteRule ^sitemap\.xml$ index.php [L]
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteRule ^(.*)(\.rss|\.atom|\.txt)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
There's a folder called book/ in my webroot that I need to be accessible from the outside. However whenever I type www.mydomain.com/book vivvo sends me to the customized 404 error page.
What rule can I use, in order to make the web server to ignore the "RewriteCond" and make users go to the right directory?
I tried this with no success:
RewriteRule book/ book/index.php [L]
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} "/book/"
RewriteRule (.*) $1 [L]
# the rest of your rules...