Rewrite Website Links (Anchors, Extensions) - .htaccess

I have several pages on my website where the home page contains some anchors links like this:
mywebsite.com/homepage.php#six
and other pages just PHP extensions like this:
mywebsite.com/otherpage.php
What shall I do if I want to remove everything after slash on home page and the PHP extension on others?

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /homepage\.php(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(\S*) [NC]
RewriteRule ^ /%1%2 [R=301,L]
RewriteRule ^/?$ /homepage.php [QSA,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA,L]

You can use this in your htaccess
RewriteEngine on
RewriteRule ^$ /homepage.php#six [NE,L]
#Remove php
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ /$1.php [NC,L]
This will rewrite
http://example.com/
to
http://example.com/homepage.php#six
And the second rule removes php extensions from files.

Related

htaccess drop php for clean SEO friendly url

I'm having trouble dropping the file extension of my webpages. I have a static site of around 10 php files. I do not have any query parameters in the url. I'd like to have SEO friendly urls.
mysite.com/other-errors instead of mysite.com/other-errors.php
I've been searching but none of the entries in my htaccess file are working. This is what i've tired, amongst other version:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_HOST} ^mysite.co.uk [NC]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [L,R=301,NC]
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I know I can go the other way:
RewriteRule ^home index.php [NC,L]
But I'd have to do this for every php file, which is a little time consuming. This must be possible. Thanks in advance.
I seem to have solved my problem I needed one of the other solutions
I tried with my redirects. Is this the correct way?
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^mysite.co.uk [NC]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [L,R=301,NC]
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Permalink
RewriteRule ^home index.php [NC,L]
RewriteRule ^e02-error e02-error.php [NC,L]
RewriteRule ^other-errors other-errors.php [NC,L]
RewriteRule ^setup-and-chemical-balance setup-and-chemical-balance.php [NC,L]

Remove "/php/" subdirectory from URL with .htaccess

I've been trying to achieve the following rewrite/redirect on a website I'm building but I'm struggling and can't find the right answer online.
I want to rewrite
http://www.example.com/php/someFile.php
To:
example.com/someFile
I have managed to achieve most of the desired result with the following code in my .htaccess root file but I need help with removing the php subdirectory:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
Any help would be hugely appreciated.
Have it this way in your site root .htaccess:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
# internally map /file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]
Make sure to test after clearing your browser cache.
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301,NE]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/php/file\.php\s [NC]
RewriteRule ^ file [R=301]
RewriteRule ^ php/file.php [L]

write htaccess to hide html file extension + make http go to https + make xyz.com go to www.xyz.com

I have the following code in a htaccess file.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain-name.com/$1 [R,L]
I don't know anything about htaccess files but this appears to do the following:
• make domain-name.com go to www.domain-name.com
and
• make http://www.domain-name.com go to https://www.domain-name.com
so everything is going to https://www.domain-name.com Which is what I want.
However how do I also hide the .html file endings? So domain-name.com/about.html becomes domain-name/about
I've found the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
But don't know how to combine the two bits of code?
You can have these rules in your Apache config or site root .htaccess:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
## hide .html extension
# To externally redirect /file.html to /file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
# To internally rewrite /file to /file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

How externally redirect url using htaccess, when url contain space, number or dash

I am new in htacces.
I want to create htaccess like stackoverflow.
Check any url of stackoverflow like "hide file extension in url by htaccess". If you put .html/.php/.asp/.abc/.xyz anything it will redirect to "hide file extension in url by htaccess" only even you put / at last it has no effect
Means I want to say url filename contain any keyboard character and it will redirect externally.
below are my current htaccess
RewriteEngine on
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
You need to use appropriate ``RewriteBase`:
RewriteEngine on
RewriteBase /folder1/folder2/
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
My final htaccess file with the help of great #anubhava
RewriteEngine on
RewriteBase /folder1/folder2/
# To externally redirect /folder1/folder2/file.html to /folder1/folder2/file.html or any extension like .php/.asp/.abcd etc
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.[a-z0-9]{2,}[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Convert Query String URL to Path

I'm trying to change http://www.mywebsite.com/en/admin/index?page=archived
to
http://www.mywebsite.com/en/admin/page/archived/
I've tried a bunch of things including:
RewriteCond %{QUERY_STRING} ^page=(.+)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/en/admin/%1? [R=301,L]
but i have always a 404 error.
My .htaccess is into admin directory
.htaccess :
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://%{HTTP_HOST}/en/admin/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/en/admin/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{THE_REQUEST} /index\?page=(.+?) [NC]
RewriteRule ^index$ /en/admin/page/%1? [NC,L,R]
RewriteRule ^page/([^/.]+)/?$ /en/admin/index?page=$1 [NC,L]
You can use the following code in admin/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\?page=([^\s]+) [NC]
RewriteRule ^ /en/admin/page/%1? [NC,L,R]
RewriteRule ^page/([^/.]+)/?$ /en/admin/index?page=$1 [NC,L]

Resources