Htaccess redirect question - .htaccess

I have a site I've written in php.
The addresses are as follows:
http://www.site.com/page.php
I'd like any request to:
www.site.com/page.php
or
www.site.com/page
To go to:
www.site.com/page/
(And force a www.)
Can anyone help me?

For domain :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
For your first directory :
RewriteRule ^/([^/]*)(\.php)? /$1/

RewriteRule ^page.php$ http://www.site.com/page/ [QSA,L]
RewriteRule ^page$ http://www.site.com/page/ [QSA,L]
Maybe this will work.

This should do what you wanted:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php

Tim's answer is the closest, but it also does adds the trailing slash to images and css...
So taking Tim's answer, and slightly editing it gives us:
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png)$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?)\.php [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/([^\s]*?[^/\s])\s [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST}/%1 ^(www\.)?(.*?)/?$
RewriteRule ^ http://www.%2/ [R=301,L]
# Assuming you want to write the request back to page.php too...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1.php
Which should work!

Related

QUERY_STRING rewrite is not being accounted for

I have the following:
RewriteEngine On
RewriteCond %{QUERY_STRING} !embed=true [NC]
RewriteRule ^/?wiki/old-page$ /wiki/new-page [L,R]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/wiki/index.php [L]
However, when I go to
/wiki/old-page
instead of redirecting to
/wiki/new-page
it redirects to
/wiki/Old-page
What am I doing wrong? Thank you in advance.

force http to https with an egsisting acces file

I dont know much about htaccess files and i was hoping you can help me.
I have this file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?d-hive.net$
RewriteCond %{REQUEST_URI} !^/d-hive/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /d-hive/$1
RewriteCond %{HTTP_HOST} ^(www.)?d-hive.net$
RewriteRule ^(/)?$ d-hive/index.php [L]
I was wondering how can i force the page to open up the HTTPS protocol instead of the HTTP protocol.
use full url e.g.
RewriteRule ^(/)?$ https://www.d-hive.net/index.php [L]
Plus, can include
RewriteCond %{HTTPS} =on [NC]
so something like,
RewriteCond %{HTTPS} =on [NC]
RewriteRule ^(/)?$ https://www.d-hive.net/index.php [L]
You can have your rules like this. Let me know how this works for.
RewriteEngine on
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?d-hive\.net$
RewriteCond %{REQUEST_URI} !^/d-hive/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /d-hive/$1
RewriteCond %{HTTP_HOST} ^(www\.)?d-hive\.net$
RewriteRule ^(/)?$ d-hive/index.php [L]

redirect m.php to m.domain.com htaccess

Is it possible to redirect
http://www.url.com/m.php?name=Bill
to
http://m.url.com/Bill/
via htaccess?
I have seen some examples but I'm not sure what
they use to accomplish that
How about:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?url\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /+m\.php\?name=([^&\ ]+)
RewriteRule ^ http://m.url.com/%1/? [L,R]
RewriteCond %{HTTP_HOST} ^m\.url\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /m.php?name=$1 [L,QSA]
assuming that both m.url.com and www.url.com point to the same document root.

301 redirection through .htaccess file not working

I'm using CodeIgniter.
in httacess file, I wrote this code to remove index.php from url:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Now I want to redirect site root to subdomain, with this code:
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
domain.com redirected to sub.domain.com successfully. but domain.com/dir to sub.domain.com/dir not work and still opening domain.com/dir.
When I removed first code, redirection worked perfect, but I need to remove index.php too.
Adding $to the end of rewritecond %{http_host} ^domain.com [nc] should fix it for you.
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Would become
rewritecond %{http_host} ^domain.com$ [nc]
rewriterule ^(.*)$ sub.domain.com/$1 [r=301,nc]
Have your rules like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Redirection through htaccess not working

I want to rewrite the URL like below:
URL: http://localhost/my_site/?file_name=sample
This is my URL I want to show it like:
URL: http://localhost/my_site/sample
that means I want to remove file_name parameter and get the parameter value and set it in URL, for setting this I have used below code:
RewriteEngine On
RewriteBase /my_site/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /?file_name=$1 [QSA,L]
But its not working, when I type http://localhost/my_site/sample its showing me the list of all sites present in my local that means its taking me to http://localhost instead of require page. What wrong I am doing?
Please help, Thanks in advance
Assuming that mysite folder is under DocumentRoot. add this to your
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^/(my_site)/([\w\d-]+)/$ [NC]
RewriteRule ^ %1/?file_name=%2 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^(my_site) $1/%2/?%1%3 [L,R]
For server side:
RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]
try this, if it works:
RewriteEngine On
RewriteBase /my_site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ file_name=/$1 [L]
caution: untested

Resources