htaccess proper redirect with trailing slash - regex

Good day!
I want to make proper redirect by htaccess, when there is GET request like this:
example.org/directory/page/
It should give example.org/page.php
My htaccess lets only redirect this kind of request
example.org/directory/page
I use this config
#remove extension
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#remove directory
RewriteRule ^directory/(.*)$ /$1 [L]
Thank you for attention!

Reorder your rules and tweak .php adding rule:
#remove directory
RewriteRule ^directory/(.*)$ /$1 [L]
#remove extension
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^.]+)/?$ $1.php [L]

Related

Why does my htaccess not rewrite properly

I am having a problem with my htaccess file, and can't figure out if it's my configuration, or something like server cache which is messing with my URLs.
My file is the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://website.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
The first rule is to move www to non-www, it works.
The second rule is to move http to https, it also works.
the third rule is to make URL /anyFile call anyFile.php, but keep a lovely URL.
The problem is that it calls anyFile.html, not PHP, and if I remove said HTML file then I get 404.
Yes it works typing anyFile.php in the URL, but I would like to not have .php in the URL.
If it is not obvious enough, it is supposed to work for any file name, not just a single one.
Any and all help is much appreciated.
Replace your .htaccess rules with this code:
Options -MultiViews
RewriteEngine On
RewriteBase /
# single rule for http->https and www removal
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
# hide .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
I'm recently learn how to do htaccess too, so I'm still a novice but I would write
RewriteRule ^([a-z]+)\/?$ helloWorld.php [NC]
to redirect it to the helloWorld.php file.
The $1 is a get parameter.

How to modrewrite in my subfolder

I have a PHP script on a subfolder of my server and it generates url like this:
http://example.com/subfolder//index.php?a=profile&u=username
Instead I would like a SEO friendly url like:
http://example.com/subfolder/profile/username.html
I'm on Apache and mod_rewrite is enabled, so I edited the .htaccess of my subfolder page:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
But it doesn't work.
This is my full .htaccess code:
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
What's wrong with my code?
Use this rule in /subfolder/.htaccess:
RewriteEngine on
RewriteBase /subfolder/
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?a=$1&u=$2 [L,QSA,NC]

Force a 404 error when ".php" is present in URL

I have this .htaccess code which hides the .php extension. It works wonders.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php
I'm now attempting to produce a 404 Not Found error when .php is present in the URL, because I want to force people to use URLs without .php. Here's the code I have now:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ /home.php [QSA,L]
RewriteRule ^(.+)\.php - [L,R=404]
I am a complete newbie to .htaccess but the code above is causing a 404 error whenever I go to a .php page, even if .php is not in the URL.
What adjustments must I make in order to make it only cause a 404 Not Found when .php is in the URL?
Rewrite conditions only apply to the immediately following rule, which means:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
only gets applied to
RewriteRule ^$ /home.php [QSA,L]
but not the rule that follows that:
RewriteRule ^(.+)\.php - [L,R=404]
The problem with this last rule is that it checks if the URI has a .php in it, which it will because you're adding the .php extension in your first rule:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php
So you need to add another condition to prevent that from happening:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ /home.php [QSA,L]
RewriteCond %{THE_REQUEST} \ /+[^\?\ ]+\.php
RewriteRule ^ - [L,R=404]

htaccess redirect rule from www.domain.com/?page=news to www.domain.com/news

i need to redirect the URL as
www.domain.com/?page=news --> www.domain.com/news
Here my htaccess file:
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^http://www\.domain\.com/news/$ /http://www.domain.com/?page=news [L]
Please suggest me the exact rule to use in .htaccess file.
Thanks in advance.
Your 2nd rule is not correct as you can't match domain name in RewriteRule pattern. That pattern only matches REQUEST_URI without domain name and query string.
Your 2nd rule should be like this:
RewriteCond %{THE_REQUEST} \s/+\?page=([^&\s]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# Specify search friendly URLs
RewriteCond %{QUERY_STRING} !(^|&)page=[^&]+
RewriteRule ^([^/]+)/?$ /?page=$1 [L,NC,QSA]
Reference: Apache mod_rewrite Introduction
Sorry, I do not know a thing about this, but I have seen a good tutorial posted by someone I am following on facebook:
http://www.9lessons.info/2013/11/htaccess-file-tutorial-and-tips.html
Hope this helps.

HTACCESS redirection with a word replacement in url

I'm having trouble with this reg expression which i belive is correct, but it is not working.
What im trying to do is redirect bunch of urls containing a specific string like this:
http://www.example.com/**undesired-string**_another-string to http://www.example.com/**new-string**_another-string
and
http://www.example.com/folder/**undesired-string**/another-string to http://www.example.com/folder/**new-string**/another-string
So i have this code in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ?
Thank you
Marwen: Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)undesired-string(.*)$ yoursite.com/$1new-string$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ yoursite.com//$1 [L,R=301]
</IfModule>
In your 'updated' code in the comments above, you had it applying the rewrite condition to the undesired-string... So if the actual file or directory was valid it would not rewrite...
Doing this though will always rewrite the undesired-string with new-string - even if its a file name... If that is fine or what you want then all you had to do was move your rewrite conditions to below the rewrite rule...
also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com
i.e.
yoursite.com/$1new-string$2
just needs to be
/$1new-string$2
which does the same thing: rewrites to the base directory of yoursite.com
now if they are going from mysite.com to yoursite.com then you woulud want to include the domain name because you are redirecting across domain names
Edit: You may also want to use:
[QSA,L,R=301]
instead of just [L,R=301]
Your regex is not really correct. Try:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
Or if this doesn't work, try:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
Explanation:
^ marks the beginning; $ marks the end; the first (..) goes to $1, the second (..) goes to $2 and so on; * is 0 or more chars; + is 1 or more chars.
To answer my own question. Laravel already redirects the trailing slashes. Problem was that Laravel was installed into a sub-directory. I added the location of the sub-directory to the redirect. My location in this case is: "/lumen/public/". See the fixed htaccess below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /lumen/public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>