Do Not Redirect for missing Folders - .htaccess

I have a website which is basically a book (it is connected to a database). Users are able to read chapters for ex: http://examplebook.com/20. The problem I have is that I do not want to create folders for all the chapters in the book, but rather extract the chapter number from the url and just connect to database and display the book pages. If I do not have a folder for each chapter it just redirects and shows not found. I see some websites that have this feature so I am assuming it is possible. Any help would be greatly appreciated.
Kind Regards,
Chewy.

You mean something like this?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
That being in the htaccess file in your document root.

Related

.htaccess sub-folders to read parent folder file while maintaining 'pretty' url

This will have been asked and will have an answer. I cannot remember what this feature is and attempts to once again learn it have come up dry.
I'm looking to place a .htaccess file within a folder where the file to read is located, anything beyond that will read that file.
Example:
www.url.com/articles/animals
www.url.com/articles/animals/safari
www.url.com/articles/animals/safari/lions...
Would display the file at
www.url.com/articles/file-to-read.php
So you can then grab the animals, safari, lions to display the necessary content*, maintaining the 'pretty' URL.
*To clarify, I'm not asking for the code to obtain the path parts, this is just an example usage of said parts.
I think you are talking about the pattern matching and rewriting to the actual file on server, something like below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^articles/(.*)$ articles/$1.php [L]
Or
RewriteRule ^articles/(.*)$ articles/filetoread.php [L]

htaccess rewrite url query string with index as base

New poster to this place but long term user.
I have a site which uses index.php and then has a viewpost.php and categorypost.php.
The viewpost is for viewing the main posts and the categorypost is for viewing posts under a category such as "webdesign".
The view post slug is made up of query string such as:
viewpost.php?postType=services&postCategory=webdesign&postTitle=somepost
I then want that to rewrite to
website/services/webdesign/somepost
First question:
Is this the correct way to do this by using 3 files or am I creating more work than needed?
Second question:
If I'm on the correct or at least an ok path, how do I go about redirecting?
I have seen about 30 or so posts around this but finding it hard to get my head around it and have ended up with 500 response or just straight redirecting.
RewriteRule ^(.*)$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]
Any help is greatly appreciated.
If these particular links can all start with the same word, it is the simplest, in this case "website". Because it avoids mistakenly using an existing folder with a missing page.
RewriteRule ^website/([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [NC,QSA,L]
But if it's not the case, you can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]

.htaccess cascading conditions

I'm trying to figure out how to manage potentially conflicting conditions in .htaccess
My setup is the following:
- I have a CMS running on a server that can be accessed through myCMSdomain.com where myCMSdomain.com would be CMS home page and myCMSdomain.com/admin would be the admin interface.
- Sites using this CMS should be pointing to myCMSdomain.com/sites/index.php
- Images for all sites are available somewhere behind myCMSdomain.com/admin/images/sitename/...
So here is how I tried to tackle this problem:
RewriteCond %{HTTP_HOST} !^(www.)?myCMSdomain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . sites/index.php
With this, all incoming requests coming from other domain names are treated correctly by the index.php file but as images are hosted somewhere else, I'd like to use a rule saying that images should be fetched somewhere else like this:
Visible url format for images is: images-[sitename]/[image_path]
Real location of the images is: admin/site/[sitename]/[image_path]
The following rule works but not in combination with the first rule
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ admin/site/$1/images/$2
Images end up calling index.php instead of using the rule I defined for them.
I have tried to excluse the image directory from the conditions but it doesn't work either:
RewriteCond %{REQUEST_URI} !(images-([a-zA-Z0-9]+)/(.*))
I might have similar issues in the future with other exception so I was wondering if there was a way to handle this.
Thanks!
Laurent
UPDATE 1:
If I use the following rule on top of all other
it works only if I'm using myCMSdomain.com domain name
if I use any other domain like anotherdomain.com, the rule leads to a http 500
RewriteRule images-([a-zA-Z0-9]+)/(.*)$ manager/site/$1/images/$2
So http://www.myCMSdomain.com/images-test/test.jpg leads me to the correct image
But http://www.anotherdomain.com/images-test/test.jpg leads me to a 500 http error code while this domain is pointing correctly to sites/index.php
UPDATE 2:
On Justin's request, here is a view on the physical directory structure on the server
/admin/
/admin/site
/admin/site/site_name/
/admin/site/site_name/images/
/sites/
/sites/js
/sites/css
You can rearrange your conditions logic.
# if www.myCMSdomain.com or myCMSdomain.com -> do nothing
RewriteCond %{HTTP_HOST} ^(?:www\.)?myCMSdomain\.com$ [NC]
RewriteRule ^ - [L]
# if we reach here, this means it's a subdomain/another domain
# images rule
RewriteRule ^images-([^/]+)/(.+)$ /admin/site/$1/images/$2 [L]
# not a file/directory -> sites/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /sites/index.php [L]
It is also possible the way you did but it would be longer to write.
Also, keep in mind that conditions (RewriteCond) are non-cumulative: they are for next rule (RewriteRule) only.
I've been struggling with this issue for some time now but at last I think I have found the issue. It looks like there was something wrong in the domain name configuration, I don't know exactly what but once I had re-saved the mapping of all domains, the htaccess worked the way it should.
In the end I have used Justin's proposition, it looks more future proof than mine.
Thanks for your help and happy new year to all
Laurent

changing link url using .htacces

This is the link http://djmobi.in/?dir=/Mobile_Ringtones&p=1&sort=1/Mobile_Ringtones.html
Please tell me how to remove ?dir= from the above link I want to make it look like below link eg.
http://www.finewap.com/Category/9497/Mobile_Ringtones.html
You must not think the way "How to remove ?dir=", but "Which link use instead of this complex one".
This is the kinf of .htaccess you'll need :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)\.html$ index.php?dir=$1 [L]
This .htaccess will redirect each url like http://www.domain.com/foo.html to http://www.domain.com/index.php?dir=foo (this will be transparent)
This is a good start, I let you search for some documentation to add your pages and sort management (we don't have enought datas to give you a working piece of code)

How to avoid URL and directory match problem when editing .htaccess file?

I have the following lines in the .htaccess file in the site directory:
RewriteEngine On
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
If it receives URL for example :
http://localhost/samplesite/johnsmith
it will rewrite it to
http://localhost/samplesite/account.php?username=johnsmith
which is fine.
The problem occurs when there is a directory named johnsmith in the site directory. then the URL is rewritten to and displayed as
http://localhost/samplesite/johnsmith/?username=johnsmith
and that is a problem. I am trying to implement account pages functionality for every user but if a user wants to register a username like some directory in the root the functionality will break? I tried adding rewrite conditions to check if the requested URL stands for an existing directory or a file :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
but I don't know how to proceed.
If someone knows a better way to do account pages functionality for users I would appreciate to give me a piece of advice on that.
Can anybody help me solve this case? Thank you!
Your RewriteCond will work if it is in the correct place. Your full .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# ---------------------------------------------------------------See info below--
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
Also, you don't need the %{QUERY_STRING}. Instead you should use the QSA flag to append the rest of the query string...
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1 [L,QSA]

Resources