I want to remove folder and .php from URL using .htaccess - .htaccess

I want to redirect
https://example.com/index.php to https://example.com
https://example.com/view/india.php to https://example.com/india
https://example.com/view/city/india/chennai.php to https://example.com/india/chennai
this is what i want to acheive anyone please send me the .htaccess code for this problem.

You'll be using
RewriteEngine on
RewriteRule ^(.*)$ $1.php

Related

How can I redirect old URLs to new URLs using htaccess? [duplicate]

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

HTACCESS: Do a url rewrite

I'm trying to use htaccess to do a url rewrite.
I would like the people to access the site in the browser using this:
http://exmaple.com/ladada
But the files are inside a subdomain:
http://ladada.exmaple.com
Would that be possible? People will access the site using the first link but in the backend it's being rewritten to the second link? Thank you so much!
mod_rewrite doc should help you.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ladada\.example\.com [NC]
RewriteRule (.*) http://example.com/ladada/$1 [L,R=301]

301 redirect a folder but exclude child folder

I need to redirect a directory "mysite.com/blog" to a new site. However, I do NOT want to redirect a child folder "mysite.com/blog/wp-admin".
Here is my htaccess code which effectively redirects the entire /blog folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
</IfModule>
I have tried the following to exclude /wp-admin, but that didn't work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/blog/wp-admin(.*)
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
Any ideas? Thank you!
UPDATE:
I tried removing the (.*) from both lines, as directed, and its still redirecting to the new site. BUT, the URL is different. Here's the URL once it redirects:
http://www.newsite.com/newblog/?redirect_to=http%3A%2F%2Foldsite.com%2Fblog%2Fwp-admin%2F
Can you tell from this URL what's going wrong? I'm not good at regex, but it looks like it's a formatting issue.

.htaccess redirect folder to a url

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

Htaccess redirect

Does anyone have a simple way of rdirected in .htaccess all pages that were once .asp to now be .php. For example index.asp is now index.php etc. Server is apache.
RewriteEngine on
RewriteRule ^(.+)\.asp$ $1.php

Resources