Htaccess remove .php and redirect specific URL - .htaccess

My htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} \.php[/\s?] [NC]
RewriteRule !^error - [R=404,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I want to do this:
http://localhost:1993/test => load test.php but hide extension
http://localhost:1993/test.php => error 404 page
http://localhost:1993/test?lang=en (en, cs, fr etc) => http://localhost:1993/test
I have this link that changes my language based on this link
<?=$lang["lang-cs"];?>
Language switch works but clicking on the link will change the URL to
http://localhost:1993/test.php and shows 404 error.
I want redirect (just stop adding .php in URL after click this link)
http://localhost:1993/test?lang=en => http://localhost:1993/test

Related

Magento2 how to remove %2f from URL

When we are hitting the URL domianname/customer/account/login/%2f we are getting plain page instead of 404 Page without Header and footer
we have try with .htacess by adding
RewriteCond %{QUERY_STRING} ^(.*)((%2F)*)
RewriteRule ^(.*)$ %{REQUEST_URI}? [R=301,L]
still showing same result
also we have try
Added
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/.*)\.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/error\.php
RewriteRule . - [R=404,L,NS]
ErrorDocument 404 /error.php?e=404
is there anything need to be done at server level ? or it can be handle through .htaccess ?
[1]: https://i.stack.imgur.com/qycvk.pngenter code here

Redirection Htaccess without a trailing slash

I would like to redirect all URL having a trailing slash referenced by google to the new url without a trailing slash.
Example :
http://example.com/toto/ ===> to http://example.com/toto
Warning I already rewrite the rule to avoid the .html extension also !
Here is my existing code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteCond %{REQUEST_URI} (.+)$
RewriteRule ^ %1 [R=301,L]
</IfModule>
I got the following error :
Not Found
The requested URL /example/.html was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The objectif of the topic was to find a solution to make a redirection of existing URL listed by search engine like google to not lose the current SEO after making a complete refresh of the web site (Wordpress to bootstrap).
The first objective was to redirect all url trafics like http://example.com/toto/ (google) ===> to http://example.com/toto.
Unfortunatly I understood that making the reverse is more powerful to not lose my existing listing on google (sitemap & robots)
So I have decided to review all the code in order to have the following result by deleting the html extension and adding a trailing slash at the end of each url :
http://example.com/toto.html ===> to http://example.com/toto ==> http://example.com/toto/
Below is the code HTaccess that I used to make this possible :
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash at the end /dir/foo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# delete the extension .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [L,QSA]
I have also added the code line below in each html page to enforce it :
<base href="http://example.com/">
Hoping to help any readers/coders in the same situation

How "not to remove 'php' extension from specific page?

I am newbie in coding and need your help. I have hidden my .php extension via .htaccess with this code:
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
But there is one page (submit.php) where .php extension is required to work my code in that page, otherwise it shows a blank page. Is there any way around to whitelist specific page form not to remove .php extension?
At the top of your htaccess file ,add the following rule :
RewriteEngine on
RewriteRule ^submit\.php$ - [L]
This will exclude the /submit.php URI from your rules.

Redirect removing (.htm) extension without using RewriteRule for a single page

I tried this simple .htaccess redirect to remove the (.htm) extension from a single page.
Redirect 301 /mypage.htm http://www.mydomain.co.uk/mypage
However it returned a 404 page not found?
Dont use redirect, use mod-rewrite to rewrite your url.
Try :
RewriteEngine on
#1)redirect "/mypage.htm" to "/mypage"
RewriteCond %{THE_REQUEST} /mypage\.htm [NC]
RewriteRule ^ /mypage [NC,L,R]
#2)dont rewrite existing dirs and files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#3)rewrite "/mypage" to "/mypage.php"
RewriteRule ^mypage/?$ /mypage.htm [NC,L]

htaccess language redirect with manual language selector

I use WPML to translate my Wordpress site, I don't want to use a Jquery Language redirect but an htaccess one.
http://example.com is the default EN version
http://example.com/de is the german one
The problem is when you're redirected to the /de/ you can't navigate manually to the EN version.
Here is my code
## Language Detection
#The 'Accept-Language' header starts with 'de'
#and the test is case-insensitive ([NC])
RewriteCond %{HTTP:Accept-Language} ^de [NC]
#If not already redirected
RewriteCond %{REQUEST_URI} !^/de/ [NC] # ADDED
#Redirect user to /de/ address
#sending 301 (Moved Permanently) HTTP status code
RewriteRule ^$ /de/ [L,R=301]
#For every other language use English
RewriteRule ^$ - [L] # MODIFIED
I am not sure why you don't use the inbuilt Browser Language redirection WPML provides?
https://wpml.org/documentation/getting-started-guide/language-setup/automatic-redirect-based-on-browser-language/
Using WPML you should not redirect via .htaccess, this is known to be a problematic approach.
I also found this on the forums of WPML:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect. Eg. Browser in German, redirect to /de/
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* http://your-site.com/de/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress
I found this here:
https://wpml.org/forums/topic/multiple-languages-without-cookies/
and adopted the code to your German example.
The point is, you would need to create a directory for the standard language, other wise switching languages with the Selector will redirect you to the Homepage by your .htaccess rule
Hope that helps?

Resources