htaccess pretty url and force www - apache

I've been using following code to achieve pretty urls:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?p=$1 [L]
And it is working fine.
However, I also need to force my domain to redirect to www while keeping pretty urls.
I've read through some threads that ask for redirection to www, but I am unable to implement it together with pretty url (domain.com -> www.domain.com, domain.com/news -> www.domain.com/news respectively).
Can anyone help me achieve this?

Have www redirect rule before rewrite rule you already have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?p=$1 [L,QSA]

Related

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

SEO Friendly URI using HTACCESS

I am new in PHP. I have my URL like below
http://localhost/index.php?page=20
http://localhost/user.php?id=15&localtion=delhi
http://localhost/folder/profile.php?id=15&active=today
What I am looking for convert my URL like below
http://localhost/page/20
http://localhost/user/id/15/localtion/delhi
http://localhost/folder/profile/id/15/active/today
I am trying from many hours to make it working with .htaccess file but no luck yet
one of little working code is like below
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
its removing .php from URL like below
http://localhost/index?page=15
I have checked many questions and answer in stackoverflow like below
https://stackoverflow.com/questions/41390397/htaccess-seo-friendly-url
https://stackoverflow.com/questions/41439512/php-url-replace-question-mark-and-parameter-with-slash
https://stackoverflow.com/questions/12451886/htaccess-for-friendly-url-with-multiple-variables
but not able to achieve my goal. Let me know if anyone expert here can help me for doing same and still my php code work fine without any issue.
Thanks a lot!
With your shown samples, please try following htaccess rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for external rewrite.
##Rule to handle from http://localhost/index.php?page=20 to http://localhost/page/20
RewriteCond %{THE_REQUEST} \s/index\.php\?([\w-]+)=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/user.php?id=15&localtion=delhi TO http://localhost/user/id/15/localtion/delhi
RewriteCond %{THE_REQUEST} \s/(user)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5? [R=301,L]
##Rule for external rewrite.
##Rule to handle from http://localhost/folder/profile.php?id=15&active=today To http://localhost/folder/profile/id/15/active/today
RewriteCond %{THE_REQUEST} \s/(folder)/(profile)\.php\?([\w-]+)=(\d+)&([\w-]+)=([\w-]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4/%5/%6? [R=301,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2.php?$3=$4&$6=$6 [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1.php?$2=$3&$4=$5 [QSA,L]
##internal rewrite rule to handle files internally.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?$1=$2 [QSA,L]

.htaccess https to http redirect conflict with existing rule

Currently we have this rule in .htaccess
RewriteEngine on <br>
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]
What this does is for any visit we will redirect to subfolder call site if the page not found. And when this happen the url will not show site as subfolder.
Example if we have
/rootFolder/site/temp.html this will show in url as
http://www.domain.com/temp.html
This is working fine but now we need to add https redirect if user visit site.
This is the new rule I came up with
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://%{HTTP_HOST}/site/$1
RewriteRule ^(/)?$ https://%{HTTP_HOST}/site/index [L]
But the issue with this one is now url will show site subfolder
https://www.domain.com/site/temp.html
How can I achieve so that if user does
http://www.domain.com/temp.html it will find temp.html in site subfolder and redirect to https and url will only show
https://www.domain.com/temp.html
Thanks
How about if you do it in two passes, always redirecting whatever was requested to https, then running your rewrite rules, like so:
RewriteCond %{HTTPS} off
#301 flag redirects instead of rewriting
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#leaving your original rules as is
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]

htaccess seo friendly urls and redirect

I'm currently trying to make urls more SEO friendly, the problem is they are only half working.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1
That's my code. It works to an extent, but I can load the page from both mysite.com/index.php?name=name and mysite.com/name/ ; where as I only want mysite.com/name/ to load and I want mysite.com/index.php?name=name to redirect to mysite.com/name/
Any suggestions? Thanks.
Try this one
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?name=([^&\s]*)\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?name=$1 [L]

Use specific PHP file for specific directories

I am trying to create mod_rewrite rules that rewrite based on the first level directory name plus a failover to rewrite to a standard file in case none of the directory names are matched.
Example:
I have units.php, models.php and other.php. The other.php file should handle all non-assigned requests.
http://www.mydomain.com/units/4435
Should redirect to /units.php?id=4435
http://www.mydomain.com/models/594
Should redirect to /models.php?id=594
http://www.mydomain.com/anything
Should redirect to /other.php?id=anything
http://www.mydomain.com/anything/893
Should redirect to /other.php?id=anything/893
Let me know if this makes sense. I am unsure of how to structure the rules and conditions to achieve what I want.
This is what I have tried to sfar. It works for URLs starting with 'units' or 'models' but I get a 500 Error if I try any other URL:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(units)/(.+)$ /units.php?id=$2 [L,NC]
RewriteRule ^(models)/(.+)$ /models.php?id=$2 [L,NC]
RewriteRule ^(.+)$ /other.php?id=$2 [L,NC]
I would suggest this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(units|models)/([0-9]+)/?$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(units|models)/[0-9]+/?$
RewriteRule ^/[^/]+/([0-9]+)/?$ /other.php?id=$1 [L,QSA]