.htaccess file doesn't work for home page - .htaccess

Following .htaccess doesn't work for root level page.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^demo\-blog/(.*)$ demo\-blog/index.php/$1 [L]
If I access http://example.com/demo-blog/blog/publish.html then it works by executing file on http://example.com/demo-blog/index.php/blog/publish.html but when I access http://example.com/demo-blog/ it doesn't load the page from http://example.com/demo-blog/index.php. The .htaccess file is on the domain root. http://example.com/
Please let me know why it doesn't rewrite the url for the home page.

Change your rule to this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^demo-blog(/.*|)$ demo-blog/index.php/$1 [L,NC]

Related

How can change url link with htaccess

I have PHP file page.php, and with this file I dynamically change content. So how can I make with htaccess to change link from www.example.com/page.php?page=somepage to www.example.com/somepage?
Add the following code to your htaccess file :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /page.php?pape=$1 [NC,L]
This will rewrite your url
example.come/somepage
to
example.come/page.php?page=somepage

Use .htaccess file to check folder for file and redirect if not found

I want to use .htaccess files so that if someone visits an old URL on our new website such as website.com/oldpage.asp .htaccess first checks to see if website.com/oldsite/oldpage.asp is on our server. And if no file is there redirect them to old.website.com/oldpage.asp where a copy of our old website exists.
For some reason only the .htaccess file in the /oldsite/ folder is executing.
Here is my .htaccess file for my site root which checks if the file exists on the new site and if not redirects to /oldsite/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /oldsite/$1 [NC,QSA,L]
Then in /oldsite/ its supposed to check if the file again exists, and if not redirect again.
RewriteEngine On
RewriteOptions inherit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,NC,QSA,L]
You don't need 2 .htaccess for this.
Have this rule in site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/oldsite/$1 -f
RewriteRule ^(.*)$ oldsite/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://old.website.com/$1 [R=301,L]
Then remove /oldsite/.htaccess and test this after clearing your browser cache.

.htacces redirect to new domain if file or folder not found

if my site is domain.com and if i go to domain.com/1 and the folder 1 dont exist can i redirect via .htacces to domain2.com/1
if file or folder exist i like to show that file or folder but if not exist then to redirect to new domain
example: if file.html dont exist in my server then
example.com/file.html redirect to newdomain.com/file.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ newdomain.com/$1
You need to include the http:// bit:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R]
otherwise, mod_rewrite will interpret that as a URL or file path and attempt to look for a directory called "newdomain.com". If you want the redirect to be permanent, change the R flag to R=301.

Htaccess - redirect 'files' with no file extension

I want to redirect only php files and files with no file extension like these:
http://www.test.com/test.php
http://www.test.com/TEST
I have this htaccess script, which redirects php files to target.php:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^\.]+\.php$ /target.php [L,NC]
Now I need to add files with no extension to this redirect.
Thanks for help!
These rules should do the job.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^\.]+\.php$ /target.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]*[^/\.]$ /target.php [L,NC]
The redirect will work for http://www.test.com/TEST as long as you do not have folder or file named TEST in your site.

Help in configuring .htaccess to exclude a path from rewriting

I have a root folder with the following .htaccess configuration
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
What I need is, I have installed a wordpress site in a sub directory 'blog/'. Now I am able to access the home page of the blog since I have givien RewriteCond %{REQUEST_FILENAME} !-d. But clicking on a post will redirect me to the index file in root folder. How can I solve this. Please help. Thanks in advance.
Add a RewriteCond to exclude paths which begin with "blog/"
Something like (added some existant rule for context)
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?blog/
Not tested, but you get the idea

Resources