RewriteEngine - How to ignore requests for folders that already exist - .htaccess

What I would like is if the user enters:
www.mysite.com/test
This would redirect to a page that would search my db for an event called test and if the event exists show the event else redirect to a search page.
I've nearly managed to get this to work - so close yet so far...
This is where I've got to:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ show_events.php?q=$1 [NC,L]
It works in that it redirects to the show_events.php page which searches the db for an exact match and displays the event if it finds one, else it redirects to a search page. I'm very pleased with this.
The problem is when the folder (www.mysite.co.uk/test/) exists already an error page shows saying "this page isn't working". The folder just contains a HTML page that displays the word "hello". If you type www.mysite.co.uk/test/index.html the page displays ok so it's not like it isn't working.
I've concluded the search page works fine but the RewriteEngine code doesn't.
This post looked like it would solve my problem but not quite:
Apache RewriteEngine - Rewrite anything that not exists to index.php
I thought that by adding DPI on the end of the RewriteRule it might solve the problem but it doesn't.
I really don't understand RewriteEngine and am just groping around in the dark using trial and error, can someone help me?

The problem is that your rule also rewrites existent directories to the destination file. You need to put a condition to exclude directories from the rule :
RewriteEngine On
RewriteCond ℅{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ show_events.php?q=$1 [NC,L]
The RewriteCondition will prevent your existent directories from being rewritten to the rule's destination url.

You can simply add a condition to avoid touching existing folders
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ /show_events.php?q=$1 [NC,L]

Related

htaccess URL rewrite pattern format

I've had a good look through all the other htaccess url rewrite questions, but all of them deal with the reverse of my problem.
The site I am working on takes content from child pages in WordPress and presents them as anchored sections on the parent page. The problem for the site now is that if Google (or the built in search, though that can probably be done in the templates) links to one of the child pages it will direct users to the single page rather than the correct section of the parent page.
I was hoping to come up with a URL rewrite pattern which would reformat the URL to what I need, but it doesn't seem to work... and I know that's because I have done it wrong!
The URL which needs to be rewritten is something like:
website.com/parent-page/child-page/
I need this to be rewritten to:
website.com/parent-page/?subpage=child-page
My initial stab at it looks like this, but I know I've misunderstood something about how to format the pattern.
RewriteRule ^/$1?subpage=$2 ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
Can anyone help format this correctly or point out where I'm going wrong?
You can use:
RewriteEngine on
# If the request is not for a valid file/directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/?subpage=$2 [QSA,L]
No initial / in htaccess RewriteRule first uri
And optional final /
RewriteRule ^([^/]+)/([^/]+)/$ /$1/?subpage=$2 [L]
The htaccess rewrites didn't seem to work, so I've created a PHP redirect instead by taking the page slug, page parent permalink and mashing the two together.
Not the most elegant solution, I expect, but it works.

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

.htaccess RewriteRule not redirecting from real folder

I'm still a bit fuzzy on the working of .htaccess, and I've looked around but I can't find anything to help this specific issue.
EDIT: I realize there are other questions that seem like they cover this issue, but I checked some and they didn't seem to offer any help I could understand, and I didn't want to hijack them with my own issues.
This is what I have:
Options +FollowSymLinks
#RewriteBase /
RewriteEngine on
RewriteRule /mp3/(.*) http://old.domain.com/mp3/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
As you can see from the last line, the string typed after the server name is actually a URL parameter and depending on that parameter, different content is pulled from the database and that page is displayed on the site.
The problem I'm having is that the client has a content page called "podcast", so they would go to site.com/podcast which should quietly redirect to site.com/index.php?=podcast and load the content for that page. Unfortunately, the client also has a real site.com/podcast/ folder on their server. Because of this, the rewrite is ignored and the browser attempts to load that folder. It either shows a file listing or a forbidden error if I disable the listing.
After some research (I'm still new to htaccess), I learned that the two lines prior disable the rewrite if the path points to an actual file or folder. Unfortunately, commenting out the one with !-d doesn't seem to have any effect, and commenting out both gives me a server error.
Admittedly, part of the problem here was lack of foresight. URL rewrites should have been planned before everything else was put together, but it wasn't until the site was basically completed that I was notified that the client wants "Friendly URLs" that don't include the ?p= part. Regardless, perhaps there is a way to fix this.
Is there some .htaccess trickery I can use that will force the rewrite even if the URL entered points to a folder (not a specific file) that actually exists? As stated before, removing the !-d doesn't seem to help, although I'm not sure why. Perhaps I misunderstand its purpose.
Thank you for any help, and please be lenient with me if I overlooked something obvious. This is an issue presenting itself on the client's live site right now so I feel a little rushed in solving it. Thanks again.
OH YEAH, and the solution can't be specific to /podcast. The way the client's site is set up, when they want to create a new subpage for the site, a new name is saved for that content based on their title for the page and it is possible (unlikely, but still possible) that another page can be created with a name that matches an existing folder on the server.
Here is a note from mod_rewrite documentation:
By default, mod_rewrite will ignore URLs that map to a directory on
disk but lack a trailing slash, in the expectation that the mod_dir
module will issue the client with a redirect to the canonical URL with
a trailing slash.
This explains why mod_rewrite ignores the URL /podcast. I would suggest that you rename physical directories so that do do not (accidentally) match article names.
Another option would be to disable the DirectorySlash setting. This will prevent Apache from redirecting /podcast to /podcast/.
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
Be warned that disabling this setting has side effects. Read through the documentation first.
Change the following line of code:
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
to
RewriteRule ^(podcast([^?]*)) index.php?p=$1 [L,NC]

htaccess query. 2 rewrites rules for 2 home pages

I'm hoping someone might be able to help with a problem I'm having due to my lack of experience and knowledge with htaccess.
What we're doing is running IP Boards forum software and wordpress both in the root directory. The IPB has the index.php file (because of having indexed url's) and the new Wordpress's index.php file has been renamed to blog.php.
At the very top of the htaccess file we've added: DirectoryIndex blog.php index.php - so the new wordpress opens first.
The problem I'm having is trying to have 2 rewriterules in the htaccess file for the friendly urls from the forum software and also the permalinks for the new wordpress.
I can only seem to have one or the other.
Please could anyone tell me, or point me in the right direction to get both working.
This is what I'm doing so far but sadly no joy, but works fine if we remove one of the condition and rewrites.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog.php [L]
</IfModule>
Many thanks in advance.
Not sure if you still want an answer. Either way, if you are trying to go to two different pages you need some way of distinguishing them.
DirectoryIndex basically tells the default file (and order) when entering a directory. So http://host.com/ with both blog.php and index.php in the directory will serve up blog.php because it is first in the list you gave the server. If there is only index.php, it will serve that. If neither (and you don't have anything else in the list) it will throw a 404 because no default file is found.
EDIT: it will try to list contents if not found. My bad. If you don't allow directory listing, then it will probably show an error code. To turn off directory listing look in options: http://httpd.apache.org/docs/current/mod/core.html#options
http://httpd.apache.org/docs/current/mod/mod_dir.html
Your rewrite rules seem to kind of want do the same thing in a different order. If you request http://host.com/a and a is not a file or directory (according to the conditions) it will go to index.php.. if index.php doesn't exist, then it will loop until the server catches it, because you don't check that. So, that means the second set of conditions don't do anything, because either index.php exists or it doesn't and the next set probably won't really be reached unless it does.
You need to decide how to differentiate the two (/blog/ for the blog.php and / for index.php or something) and make one of them the default. If you want to randomize it, I would suggest doing that through PHP.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
.. to redirect from root to /forums/ through htaccess try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/forums/
RewriteRule ^(.*)$ /forums/$1 [L]

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Resources