Redirect with htaccess with variable - .htaccess

I would like to redirect all my php files to without php extension.
Like news.php to news or articles.php to articles. I know about this htacces command:
Redirect 301 /news.php /news
But I would like to make with variable, something like this but does not work:
Redirect 301 ^(.*)$ /$1.php

You can use the rewrite module for this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule 301 ^(.*)\.php$ $1 [L,QSA]

You will need a redirect rule to remove .php extension and a rewrite rule to add .php extension silently. Try these 2 rules in your root .htaccess:
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R,L]
# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

.htaccess 301 redirect only work for 1 new link

I have .htaccess file with redirect 301 on it and placed right under the rewriteengine
<IfModule mod_rewrite.c>
RewriteEngine On
#now this is the redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
</IfModule>
only 2nd redirect works, the one with the get method are not redirected, am i doing wrong?
EDITED :
this is my entire mod rewrite :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Permanent URL redirect
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
# Permanent URL redirect
Redirect 301 /blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah
Redirect 301 /blog.php?slug=konsolidasi-tanah-frequently-asked-questions /blog/2021/02/11/pengertian-konsolidasi-tanah
RewriteRule ^blog/2021/02/11/pengertian-konsolidasi-tanah /blog.php?slug=konsolidasi-tanah-frequently-asked-questions
It is usually not a good idea to mix RewriteRule and Redirect 301 directives. They can conflict with each other in unexpected ways. You depend of RewriteRule so you should implement your redirects with more of them.
Redirect 301 can't redirect based on query strings (?...) in the URL, so you need to implement RewriteRules for that redirect anyway.
When you have rules for redirecting specific URLs, they should go at the top of the .htaccess file so that they take precedence over the other more general rules.
I would recommend disabling the directory index because I fear it would conflict with your RewriteRule ^index\.php$ / [R=301,L] rule.
I don't see RewriteEngine On in your .htaccess file, despite that your snippet you posted to start with has it.
Try this as your .htaccess:
# Disable index.html, index.php default functionality
DirectoryIndex disabled
RewriteEngine On
# Permanent URL redirect
RewriteCond %{QUERY_STRING} ^slug=konsolidasi-tanah-frequently-asked-questions$
RewriteRule ^blog\.php$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]
RewriteRule ^blog/2021/02/11/konsolidasi-tanah-frequently-asked-questions$ https://jpi.or.id/blog/2021/02/11/pengertian-konsolidasi-tanah [R=301,L]
# Forward URLs without .php extension to existing PHP file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
# Redirect index.php URLs to the directory
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]
# Use index.php as a front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

set up 301 redirect from subdirectory to specific html file

I'm running apache on ubuntu 14.04 and trying to set up 301 redirects. Redirection is working, but not as expected. Here's what I have:
#REDIRECTS
Options +FollowSymLinks
RewriteEngine On
# REMOVES INDEX.PHP
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# REDIRECT SPECIFIC PAGES
Redirect 301 /main http://mikeheavers.com
Redirect 301 /main/ http://mikeheavers.com
Redirect 301 /main/code http://mikeheavers.com/tutorials.html
Redirect 301 /main/code/ http://mikeheavers.com/tutorials.html
The first two redirect 301s work, but the rest, such as /main/code, try to redirect to http://mikeheavers.com/code and note http://mikeheavers.com/tutorials.html. What am I doing wrong?
update: note that I need to be able to redirect urls both containing and not containing the trailing slash
Don't mix mod_rewrite rules with mod_alias that has Redirect directive. Use this:
#REDIRECTS
Options +FollowSymLinks
RewriteEngine On
# REDIRECT SPECIFIC PAGES
RewriteRule ^main/?$ http://mikeheavers.com [L,NC,R=301]
RewriteRule ^main/code/?$ http://mikeheavers.com/tutorials.html [L,NC,R=301]
# REMOVES INDEX.PHP
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Make sure to clear your browser cache before testing this change.

.htaccess redirects without extension in url

I'm hiding my file extensions with
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
When I navigate to this link
http://localhost/website/profile?user=user01&nofollow=1
I'm redirected here
http://example.com/folder/profile?user=user01
I've taken .php out of the url. Why does it redirect me?
Have it this way inside /website/.htaccess:
RewriteEngine On
RewriteBase /website/
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php[?\s] [NC]
RewriteRule ^(.+)\.php$ $1 [R=301,L,NC]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]
Make sure to clear your browser cache before testing this.
Try using this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

SSL and remove extension .html via HTACCESS without redirect chains

I see that all these scripts for removing .html extension seem not to work via SSL. I solved the issue with new script but now i am creating a 2 step redirect chain which i do not like for SEO reasons - and with your help :) i hope to get it back to a redirect step / hop.
My script is
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.carpro.ro/$1 [R=301,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
but it seems to create a cascade of redirects.
Is there any better version which
301 http to https for all pages of the site
redirect non WWW to WWW
remove .html extension and do not leave a trailing /
The script above works but with redirect cascades which i do not like
http://www.domain.com/something.html
does a 301 Redirect
https://www.domain.com/something.html
then again a 301 Redirect
https://www.domain.com/something
rather than
http://www.domain.com/something.html
a single redirect to
https://www.domain.com/something
Any ideas on optimising this?
Try this code:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.carpro.ro%{REQUEST_URI} [NE,R=302,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.+?)\.html$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]
You can try below Rewrite Rules to eliminate html extension
RewriteRule ^(.*).html$ /$1 [R=301,L]
And the following rewrite rule for trailing / removal
RewriteRule ^(.*)/$ /$1 [R=301,L]
Hope this helps

how to redirect my www.example.com/index.html file www.example.com/index

how to redirect my www.example.com/index.html file www.example.com/index using .htacces
and if my url is like www.example.com/test1/index.htm
or
ww.example.com/test/test2/index.htm
how to redirect the pages with out extensions
# Redirect everyone but for me
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^127\.0\.0\.1
RewriteRule .* http://www.example.com [R=302,L]
above code i found in web but it's not working ... how to specified .htacces rules
You can use these rules in root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]

Resources