RewriteCond and RewriteRule newbie - .htaccess

I'm taking over a website for a client that is running on a custom built CMS (that I didn't write). I don't mess with .htaccess files usually because a lot of the hosting I do is on IIS, or I used WordPress as a CMS and don't have to worry about messing with the .htaccess file. Here's the contents of the file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ framework.php?%{QUERY_STRING}&resource=$1& [L]
I get what it's doing (sending all requests through the framework.php file). The client wants a WordPress blog added to their site. I'm placing it in a /blog/ folder. The problem is that because of the rewrite rules and conditions in the .htaccess file whenever I try to go /blog/ the other CMS freaks out because it doesn't like me trying to go there. My question is how do I write a rule/cond that tells apache to send all requests made to the /blog/ folder to the /blog/ folder, but keep all other requests piped through the framework.php file like it is now? Any help is appreciated, thanks!

You should be able to tell the main .htaccess file to ignore any /blog links by adding a RewriteCond to the main rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/blog(/?)
RewriteRule ^(.*)$ framework.php?%{QUERY_STRING}&resource=$1& [L]
A /blog link won't be sent to framework.php, and should serve from the /blog directory normally. The Wordpress .htaccess file in that directory will be invoked as it normally would, and should be able to handle Wordpress links from there.

Related

Is there a way to properly redirect or rewrite vistors in my drupal 8 website to a subfolder?

I have the following code in my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysitedomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /web/$1 [L]
I'm currently using CPanel and CPanel does not allow you to set a new Document Root. So have my drupal files in my CPanel "public_html" directory with the composer.json files etc..and web directory that has all the Drupal related files. I am trying to get the site vistors to rewrite or redirect to www.mysitedomain.com/web for subsequent pages.. I tried the code below but does not seem to work.. Am i missing something?
To be specific.... I need the site to 1. load www.mysitedomain.com/web when www.mysitedomain.com is requested. 2. And ensure /web is is front of every subsequent request page request within the site (ie. www.mysitedomain.com/web/products should load instead of www.mysitedomain.com/products)
I am not sure what your question is, but I think you need something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
What it does: If the URL does not resolve to a file or directory in the present directory, put index.php in front of it.
I'd assume your index.php would then include the Drupal framework.

htaccess rewrite old php files to CodeIgniter file

I'm trying to redirect old site files toward new CodeIgniter site with .htaccess.
So, my old file
www.mysite.com/news/new?id=123
have to redirect to
www.mysite.com/news/new/123
For now, my .htaccess has inside usually code to remove index.php and it works fine.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php?/$1
But when I try the old file redirect nothing works.
I think the issue's about CodeIgniter, becouse if I make a simple redirect to an external file, like phpinfo.php it works fine, but if a try the redirect to index.php (of CodeIgniter) it doesn't load index.php but try to load other. Also does if I delete .htaccess code for index.php (see upside).
So,
RewriteRule news/new.php phpinfo.php [L] # it works
but
RewriteRule news/new.php index.php [L] # it doesn't work
Codeigniter use REQUEST_URI Apache variable that can not change with RewriteRule and without redirection. So you can use _remap function in news controller to call your custom method.
Other way that can be doing with htaccess is enabling query string.
Try
RewriteRule news/new\?id=(.*)$ index.php/news/new/$1 [L]

.htaccess redirect if a folder name in url of CakePHP

Building on this question: .htaccess redirect if folder name in url I'd like to know how to apply the .htaccess rules regarded there, in the .htaccess of the webroot of the CakePHP app. without generating server errors or missing the CakePHP rewrite functionality.
My CakePHP version is 1.2.10 and the .htaccess of the webroot is as follows:
#app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
AddType audio/mpeg mp3
I need this because the application has an audio folder that has sub-folders with mp3s files that the application play them using soundmanager2. I want when some one try to access a mp3 file directly or from external website to be redirected to another page, but keeping the soundmanager2 able to play the file from my website page. So suppose the following:
http://mycake.com/audio/folderX/fileY.mp3
I want any access to http://mycake.com/audio/* to be redirected to http://mycake.com/mistakeSound.mp3
In a htaccess file the order of the rules is important, if the rules and conditions apply to the same requests. If the request URLs are processed by different rules, you can place the rules in any order.
In your case, you have real files in the other question and non-existing files and directories here in this question
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This means, you can insert the other rules before or after the CakePHP rules.
I found the solution which is depend on placing .htaccess file in the regarded audio folder. The syntax of that .htaccess should be as follows:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mycake\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mycake\.com.*$ [NC]
RewriteRule \.(gif|jpg|mp3)$ - [R=401]
ErrorDocument 401 /e401.html
When someone try to access any mp3 file via, for example, http://mycake.com/audio/folderX/fileY.mp3. A 401 error will be generated.
However, I noticed that FDM is able to access and download this file. The redirect works only for web browsers.

Rewrite Rule to include redirecting for an existing directory

I have the .htaccess script:
DirectoryIndex index.php
Options -Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L,QSA]
which will redirect all requests into the index.php script, except for existing files or directories that exist. However, I have the directory /lib which contains the libraries for the webapp, and I would like to extend the rewrite rule to redirect requests to /lib[/(.*)] (the directory and everything under it) into index.php as well, but so far, I have been having issues.
What lines do I need to add to my .htaccess file to add support for that rule?
p.s.
The webapp that I am doing this for currently exists on a test server, and is all stored under a higher base, so the lib URL at the moment is http://localhost:8080/c2/lib. I have not defined the /c2 part in the .htaccess as I am trying to keep it disparate, and only have the webapp do the processing.
I am aware that I could just move the /lib directory into a higher directory, but I am trying to do this with .htaccess.
You can add an additional rule, which rewrites all requests for /lib/*
RewriteRule ^lib/.*$ index.php [L]

CodeIgniter: Multiple Apps, remove .php from url

My question may look similar to others, but it's different as you'll se:
I have 5 CI apps running with the same System Folder, as described in CI User Guide.
Now, I want some .htacces file to remove just the .php from the url, like, as example: http://example.com/appName/controller/action instead of http://example.com/appName.php/controller/action (removing the .php portion of the url)
Can I do that? I have no experience with .htaccess files. Also, I'm developing the site in Windows 7 with XAMPP, to deploy the site to a LAMP server.
Thanks in advance
Try This
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) appName/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ appName.php/$1 [QSA,L]
check code igniter manual. It has a chapter for that purpose http://codeigniter.com/user_guide/general/urls.html
Removing the index.php file
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

Resources