Hide index.php from web site URL - .htaccess

I'm using htaccess rewrite engine to make urls look nice,
from www.mysite.com/index.php?pag=home to www.mysite.com/pag/home
it works fine with this rule
RewriteRule ^pag/([^/]+)$ index.php?pag=$1 [L,QSA,NC]
but when I go to www.mysite.com it redirects me to www.mysite.com/index.php
is there a way to redirect to www.mysite.com/pag/home?
I tried
redirect 301 /index.php http://www.mysite.com/pag/home
but when i try to go to www.mysite.com the browser gives my "page do not exsist error"

This is the rule that Drupal uses to make it's pretty urls, yours should be extremely similar:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Your rule only matches on urls that have pag in them, but you're really wanting to hit every url.

the problem was in this redirect rule
redirect 301 /index.htm http://www.mysite.com/index.php

Related

.htaccess redirect for https://.co.nz website to .com version(including all subpages)

I am palling to migrate my website from the .co.nz to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly without any 404 pages from .co.nz version
All https://
I want "https://www.example.co.nz/" to go to "https://www.example.com"
Which will also redirect
https://www.example.co.nz/contact to go to https://www.example.com/contact
https://www.example.co.nz/men to go to https://www.example.com/men
Many Thanks
This should do the trick:
Redirect 301 / https://www.new-example.com/
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ https://new-example.com/$1 [L,R=301]
You put this in the .htaccess file of the .co.nz domain. This will redirect all your content and subfolders to their appropriate subfolders on the new domain (so example.me/subfolder to example.com/subfolder) AND also force redirect to https part.

301 redirect: redirect urls with .php to folder

In .htaccess I want to redirect urls like this:
/products.php/a7-frames
/products.php/a6-frames
to this:
/picture-frames/a7-frames
/picture-frames/a6-frames
So need to substitute products.php with picture-frames.
After a lot of Googling I tried this:
RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1
But it doesnt work, if I enter this url: /products.php/a7-frames the browser says there are too many redirects and goes to:
/picture-frames/index
It's substituting the products.php for picture-frames which is great, but I'm not sure why it adds "index" on the end rather than the /a7-frames part of the url? How can I fix this?
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]
Make sure to clear your browser cache before testing this change.

How can I redirect everything in one sub directory using htaccess

I have a website and need everything in my subdomain to redirect to my home page.
For example I would need www.website.com/sub to redirect back to www.website.com
same with anything else within /sub
so www.website.com/sub/sdjdj/sdsd/dsd ....would also need to redirect to www.website.com
How can I achieve this with htaccess?
You can use:
RewriteEngine on
RewriteRule ^sub(?:/|$) / [NC,R,L]
You can use this in your .htaccess in the root.
RewriteEngine On
RewriteRule ^sub(?:$|/.*) / [R=302,L]
When you are sure the rule is working as intended, change to 301 in rule.

How to 301 redirect domain.com to domain.com/

What is the htaccess code to 301 redirect my homepage from www.domain.com to www.domain.com/ and domain.com should also go to www.domain.com/
the non-www to www works fine, all internal redirects work, e.g. www.domain.com/abc/index.php goes to www.domain.com/abc/
However I also want that the homepage itself goes from .com to .com/
Please advise.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
There is no such URL as http://www.domain.com. This is how we write it in the browser (or just www.domain.com) but in the background, the browser connects to www.domain.com and asks for / no matter if it is present in the URL or not.
In other words, http://www.domain.com is, in fact, a shorter way to write http://www.domain.com/.
There is no point in rewriting or redirecting www.domain.com to www.domain.com/. The most you can get if you try to do it is an infinite loop.

htaccess rule is true but site gives 404

I'm struggling to see why this rule won't work on the site.
The old url was /mobile/article/site/article-name and the new url is just /article/article-name
The rule is
RewriteRule ^mobile/article/tgc/(.*)$ /article/$1 [L,R=301]
Testing against http://htaccess.madewithlove.be/ with the whole htaccess file tells us
This rule was met, the new url is http://site.dev/article/article-name
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301
but when we try hit http://site.dev/mobile/article/site/article-name in the browser, we get a 404 rather than a redirect.
What's even more curious is that we have the same kind of rule for another url
RewriteRule ^resources/a/(.*)$ http://resources.site.dev/library/$1 [L,R=301]
Which comes after our troubled redirect and it works just peachy.
What are we missing?
Keep your rule as this:
RewriteRule ^mobile/article/site/(.*)$ /article/$1 [L,NC,R=301]

Resources