how to ignore index.php file from urlrewrite - .htaccess

This is my simple htaccess code:
RewriteRule ^books/([A-Za-z0-9.]+)$ library/search.php?zig=$1 [NC,L]
My code is working fine for the followings:
mydomain/books/math
mydomain/books/english
mydomain/books/physics.applied
as
library/search.php?zig=math
library/search.php?zig=english
library/search.php?zig=physics.applied
But my code is not working only for
mydomain/books/
it is acting as
library/search.php?zig=index.php
There is no subject named index.php. I want to remove this index.php. My search function should not work for mydomain/books/

You can add a RewriteCond to ignore all files and directories from rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([A-Za-z0-9.]+)$ library/search.php?zig=$1 [NC,L,QSA]

Related

.htaccess Rewrite Rule Understanding Problems

Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]

Replace items with RewriteRule using .htaccess

I'm trying to use the Url rewrite using "RewriteRule".
I would use this link to show a new article in my blog:
https://www.example.com/article/how-to-visit-italy-in-winter
The original script (including vars) is
https://www.example.com/post.php?idpost=how-to-visit-italy-in-winter
This is my .htaccess
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ https://www.example.com/post.php?idpost=$1 [L]
but it doesn't work, the browser redirect to:
https://www.example.com/post.php?idpost=
I'm trying to resolve about 2 days witout success :(
Thank you.
You rule looks fine except for two things . Your $1 back reference is undefined and empty as you are not capturing any URI. To capture parts of the uri you need to use Regex capture-groups (.*) .
2) You need to use an absolute path instead of the full url as your RewriteRule`s destination if you want to silently redirect the url.
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+) /post.php?idpost=$1 [L]

How to re-direct blog article URLs?

I am trying to redirect a bunch of old blog article URLs using the .htaccess file:
RedirectMatch ^/index\.php/global/article/(.*)$ http://www.mywebsite.com/blog/article/$1
This doesn't really work, however, because my CMS seems to get confused by the index.php bit and keeps adding ?symphony-page= to all the URLs.
It's probably this part that is responsible:
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Can anybody help?
Please try the following (comments included to explain):
RewriteEngine On
# First, redirect the old URIs to the new ones via a 301 redirect.
# This will allow the CMS to take over using the rules that follow.
RewriteRule ^index.php/global/article/(.+)$ /blog/article/$1 [L,R=301]
# Frontend Rewrites - for the CMS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING} [L]
Try this:
#-- Input URL >> http://www.mywebsite.com/index.php/global/article/abc
### FRONTEND REWRITE - Will ignore files and folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*\/?)$ index.php?symphony-page=$1&%{QUERY_STRING}
RewriteCond %{REQUEST_URI} ^/index\.php/global/article/(.*)$
RewriteRule ^.*$ blog/article/%1 [R=301, L]
#--Output URL >> http://www.mywebsite.com/blog/article/abc
I've tested the above with this htaccess.madewithlove and it seems like it will work just fine, hopefully it will work for you too
Screenshot:

i have removed the .php extension using .htaccess but next part will not work

htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
from dis line it will not work
RewriteRule ^blog/([a-zA-Z]+)/$ blog.php?cat=$2
My url http://localhost/Seo/blog.php?cat=SEO
i want to convert it into http://localhost/Seo/blog/cat/SEO
extension are removed but next blog part is not working
You're missing a "cat" in your rule:
RewriteRule ^blog/cat/([a-zA-Z]+)/$ blog.php?cat=$2
Since your URLs are going to look like http //localhost/Seo/blog/cat/SEO
Additionally, you'll want to put that rule before the rules that you already have.

Rewrite rule to hide folder, doesn't work right without trailing slash

i have a strange apache mod_rewrite problem. I need to hide a sub-directory from the user, but redirect every request to that sub-directory. I found several quite similar issues on stackoverflow, but nothing really fits, so i decided to post a new question.
My .htaccess looks like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ foo/$1 [QSA,L]
The document-root only contains the following folder/files:
/foo/bar/index.html
I would now expect that example.com/bar and example.com/bar/ would just show me the contents of index.html.
Instead example.com/bar/ show me the content as expected but example.com/bar redirects me with a 301 to example.com/bar/foo/ an then shows the contents. I really don't get why there is a 301 redirect in this case.
When i put something this
RewriteCond %{REQUEST_URI} !^[^.]*/$
RewriteCond %{REQUEST_URI} !^[^.]*\.html$
RewriteCond %{REQUEST_URI} !^[^.]*\.php$
RewriteRule ^(.*)$ $1/ [QSA,L]
on top of that rule it seems to work, but that would require me to list every used file extension...
Is there any other way i can omit the redirect, the folder "bar" should never be seen by an outside user.
Thanks in advance!
1st rewrite rule is redirect from /foo/(.) to ($1) and second - from (.) to $1.
just idea, this has not been tested.
Better late than never...
Got it working with a simple RewriteRule which append a / to every url that doesn't have on.
# only directories
RewriteCond %{REQUEST_FILENAME} !-f
# exclude there directories
RewriteCond %{REQUEST_URI} !^/excluded-dirs
# exclude these extensions
RewriteCond %{REQUEST_URI} !\.excluded-extension$
# exclude request that already have a /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

Resources