Rewrite all urls with .html extension except for index.html - apache

The following htaccess is removing the .html extension from our files fine eg:
/page1.html redirects to /page1
but we now cannot add folders as it is redirecting the /new-folder/index.html file to /new-folder/index
Is there any way around this?
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.website\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.website.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

Change you 301 rule that removes .html to this:
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.html[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# rewrite to dir/index.html if it exists
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/$ $1/index.html [L]
This will redirect /new-folder/index.html to /new-folder/ but will redirect /new-folder/form.html to /new-folder/form.

Related

Directing Non-Slash URL to Slash

When I enter the website.com/seo link, I want to make a 301 redirect to the website.com/seo/ link. However, I don't want it to be broken in the codes I wrote in the current htaccess.
my htacces codes:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Based on your shown samples, could you please try following. Please clear browser cache before testing your URLs
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Use only one of these, do not use together!
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
If that doesn't work, use:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\/(.*?)\/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Remove the previous rules, and try once. If it works after removing previous rules, then let me know.

www. redirect only working for homepage

I have used this in .htaccess
RewriteCond %{HTTP_HOST} ^customlogoshop.com [NC]
RewriteRule ^(.*)$ http://www.customlogoshop.com/$1 [L,R=301]
However, only my homepage customlogoshop.com gets redirected to www.customlogoshop.com. All my other pages won't redirect.
Updated:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^customlogoshop.com [NC]
RewriteRule ^(.*)$ http://www.customlogoshop.com/$1 [L,R=301]
What these rules are doing is redirecting .php to a non-extension URL. Trailing slashes are redirected to non-trailing slash URL's.
Put the redirection rule above everything else.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^customlogoshop.com [NC]
RewriteRule ^(.*)$ http://www.customlogoshop.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

htaccess mod_rewrite: Simplifying URL

I've been having trouble with the following rewrite. I'm certain mod_rewrite is enabled but not sure where I am going wrong.
I'm try to change the following pattern:
/profile/?userid=157&username=FirstL
to:
/profile/FirstL
I've tried many different rules, but the two I felt were the closest to being correct aren't working at all. My current failures below:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=$([^&\ ]+)&username=$([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
 
RewriteEngine On
RewriteRule ^([^/]*)$ /profile/?userid=$1&username=$2 [L]
Full htaccess:
Options +FollowSymLinks -Multiviews
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=$([^&\ ]+)&username=$([^&\ ]+)
RewriteRule ^ /profile/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([^/]+)/([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
RewriteBase /
DirectorySlash Off
RewriteRule ^admin$ /admin/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^admin/index.php$ /admin [R=301,L]
# remove .php
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
#Force non-www:
RewriteCond %{HTTP_HOST} www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You're losing the userID part of the URL, so when you try to internally rewrite that back, there's nothing there:
RewriteRule ^([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]
This rule says the first match is the "userid" and the second match is the "username", and you only have one match, and on top of that, it doesn't even begin with "profile".
You'll need to include the userid somewhere in the URL, otherwise there's no way to extract it.
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /profile/+\?userid=([^&\ ]+)&username=([^&\ ]+)
RewriteRule ^ /profile/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([^/]+)/([^/]+)$ /profile/?userid=$1&username=$2 [L,QSA]

url Rewrite two urls in a folder

I want to rewrite Two URLs in a folder
first is
www.example.com/mybooks/list.php?id=novel-15 to www.example.com/mybooks/novel-15 for this i have the following code in mybooks/.htaccess file [.htaccess is in mybooks folder]
RewriteEngine on
RewriteBase /mybooks/
RewriteCond %{THE_REQUEST} /list\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ list.php?id=$1 [L,QSA]
Its working fine...
Now I want to rewrite
mybooks/search.php?search_word=mystery&search_option=all&page=4 to mybooks/mystery/all/4
i want to do this with out distrub the first rewrite option how can i do this
You can use:
RewriteEngine on
RewriteBase /mybooks/
RewriteCond %{THE_REQUEST} /search\.php\?search_word=([^\s&]+)&search_option=([^\s&]+)&page=([^\s&]+) [NC]
RewriteRule ^ %1/%2/%3? [R=302,L]
RewriteCond %{THE_REQUEST} /list\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/(\w+)/(\d+)/?$ search.php?search_word=$1&search_option=$2&page=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ list.php?id=$1 [L,QSA]

.htaccess rewrites a little sluggish

I have the following .htaccess code that tells the server to point certain sub domains to to a sub folder and then also handles the rewriting of the root domains.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 ./404.php
RewriteCond %{HTTP_HOST} ^secretplace\.testwebsite\.local
RewriteRule ^(.*)$ /secretplace/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^payment\.testwebsite\.local
RewriteRule ^(.*)$ /payment/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^login\.testwebsite\.local
RewriteRule ^(.*)$ /login/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^signup\.testwebsite\.local
RewriteRule ^(.*)$ /signup/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^fbauth\.testwebsite\.local
RewriteRule ^(.*)$ /fbauth/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^masterapi\.testwebsite\.local
RewriteRule ^(.*)$ /masterapi/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.testwebsite\.local$ [NC]
RewriteRule ^(.*)$ http://testwebsite.local/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [P,NC,QSA]
RewriteCond %{HTTP_HOST} ^testwebsite\.local$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ api.php?function=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ $1-$2.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
</IfModule>
It works, but on my local apache install the page loading is real sluggish. It takes a few seconds just to switch between pages.
Just so you know the very last line of code allows me to remove the .php extension from pages. So instead of http://testwebsite.local/features.php, it loads http://testwebsite.local/features/.
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
Is there something obvious in my code that I'm doing wrong? I'm not quite sure how to debug .htaccess.
Why are you using P flag instead of L flag ? This requires mod_proxy and can slow down htaccess execution. Seems to me you don't need it in your case.
If i had to write your code myself, it would look like this
ErrorDocument 404 /404.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(secretplace|payment|login|signup|fbauth|masterapi)\. [NC]
RewriteRule ^(.*)$ /%1/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^testwebsite\.local$ [NC]
RewriteRule ^(.*)$ /account_redirect/$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^ref_([a-zA-Z0-9-]+)/?$ /index.php?ref=$1 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2&extra=$3 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /api.php?function=$1&method=$2 [L,QSA]
RewriteRule ^api/([a-zA-Z0-9-]+)/?$ /api.php?function=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ /$1-$2.php [L,QSA]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteRule ^.*$ /%1.php [L,QSA]