Using mod_rewrite() on 1and1 shared hosting - php

Update
I couldn't get this to work adding rules to the .htaccess file, in the end I used the Wordpress Rewrite class and added an add_rewrite_rule() to do the re-write which worked perfectly.
Original Post
I have been trying to put together a .htaccess rule to move parts of the path to a query string. As per https://wiki.apache.org/httpd/RewritePathInfo.
I have two folders in the root of my webspace, one for www.mydomain.com and one for test.mydomain.com. My .htaccess file is in the test.mydomain.com folder, and I want to rewrite test.mydomain.com/images/parm1/parm2 to test.mydomain.com/images/?gwpw=parm2&gwpi=parm1.
If I put garbage in the .htaccess file I get a 500 internal server error so I know the file is being read. I also have some existing rules in .htaccess from Wordpress that use mod_rewrite(), which if I comment out, stop the site redirecting to a 404 error page if you try to access test.mydomain.com/somegarbage. So I know mod_rewrite() is enabled.
I have read I need to set Options +FollowSymLinks -MultiViews, so the whole rule I have is:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/images/([^/]*)/([^/]*) /images/?gwpw=$1&gwpi=$2 [PT]
I have tried this in different parts of the file, removing the leading slash from /images/?gwpw=.... I've tried adding a rule that I thought should rewrite every request to a different page like RewriteRule ^/* /somewhere but I can't seem to get any rewrite to work.
As this is a shared server I can't access httpd.conf.
My entire .htaccess file currently looks like this:
<IfModule php5_module>
php_flag session.cookie_httponly on
</IfModule>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/images/([^/]*)/([^/]*) /images/?gwpw=$1&gwpi=$2 [PT]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
I am a complete newbie when it comes to .htaccess rules, so any help would be greatly appreciated. I'm clearly doing something wrong but I can't see the wood for the trees anymore!

Related

.htaccess - DirectoryIndex appears to have no effect

I'm trying to adapt the .htaccess file in the root of my CodeIgniter installation so I don't have to include index.php in all the URLs leading to my controllers.
This is the content of my .htaccess file:
<IfModule authz_core_module>
Require all granted
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
DirectoryIndex login.php
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteCond $(REQUEST_FILENAME) !-f
RewriteCond $(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1
As you can see, it's the default .htaccess file with some rewrite rules for mod_rewrite and a DirectoryIndex directive appended to it. Mod_rewrite is enabled (I've checked this using apachectl -M).
Now, I'm running Apache 2.4.18 on Ubuntu 16.04 LTS and I am using CodeIgniter 3.1.6. The problem is that when I type in no URL I get redirected to CodeIgniters index.php instead of my wanted login.php. I'd rather get the DirectoryIndex directive working instead of transferring the contents of login.php into index.php for obvious reasons. My mod_rewrite rules are meant to route URLs to existing files "through" index.php without having to include index.php in their URL. This way, my URLs are cleaner but I can still use CodeIgniter's facilities.
My master Apache configuration obviously allows me to use .htaccess files (since CodeIgniter needs this too) and the URL rewrite directives work just fine. Why does the DirectoryIndex not seem to do anything no matter where I place it inside the file?
Check out if you have AllowOverride instruction set to All -- it controls if .htaccess file has any power.
There are the answers on how to set it: How to Set AllowOverride all
I fixed the problem but not in an elegant way.
It appears to me that mod_rewrite just doesn't work (correctly) when you use it in a .htaccess file of a subdirectory of your DocumentRoot.
My CodeIgniter installation was in a subdirectory called "CodeIgniter" inside the DocumentRoot. I made two .htaccess files:
DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %1 !^index.php
RewriteRule ^(.*)$ /CodeIgniter/index.php [L]
DocumentRoot/CodeIgniter/.htaccess:
<IfModule authz_core_module>
Require all granted
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
Options FollowSymLinks
DirectoryIndex login.php
This seems to be doing the trick for me
Moral of the story: do all of your URL rewriting using mod_rewrite in the .htaccess located directly in your DocumentRoot

symfony2 - remove /web from urls

I'm managing a website written in symfony 2.2, hosted on a web hosting service.
I'm pretty much a complete neophite in web development, so I'm probably making some stupid mistake.
Anyway, I want to be able to access the site without the /web part.
Example: 'domain.com/symfony_site/web/mypage' -> 'domain.com/symfony_site/mypage'.
Let me say from the start: I cannot manually configure apache on the server, I only have FTP access to it.
I think I'm left with .htaccess files solution.
I have tried each and everyone of the solutions I found on the internet (most of them from this site) and none worked.
And yes, mod_rewrite is enabled.
I feel like the simplest solution would be to add a .htaccess file in the root directory of the site (the one which also contains the /web folder), with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /web/$1 [QSA,L]
</IfModule>
And this does seem to redirect properly, but then I get a "The requested URL web/mypage was not found on this server." if I visit 'domain.com/symfony_site/mypage'.
The desired home page 'domain.com/symfony_site' intead gives a "You don't have permission to access /symfony_site/ on this server.", so it looks like this is not even redirecting properly.
First, you need to move the .htaccess from the web directory to the root directory (the DocumentRoot).
Then, open the moved .htaccess, search all occurrences of /app and replace them by /web/app.
The .htaccess (without comments) should looks like :
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/web/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /web/app.php/
</IfModule>
</IfModule>
Now your application can be correctly browsed using the project root directory as DocumentRoot.
This change may involve to adapt the way of loading your assets.
NOTE: It's a quick-and-dirty alternative assuming you cannot change the DocumentRoot of your application to make it points to the web directory.
a symfony2 app is designed to have the document-root at web/
here is an example configuration for apache vhost.
<VirtualHost *:80>
ServerName your.domain
DocumentRoot "/path/symfony/web"
<Directory "/path/symfony/web">
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
so your rewrite rule is not necessary

Simple RewriteRule do not work at php/apache in htaccess

I do not know why, but this simple Rule will not work.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^rubbellos\.png/$ rubbellos/rubbelbild_png.php [NC,L]
RewriteRule ^rubbellos\.css/$ rubbellos/rubbellos_css.php [NC,L]
</IfModule>
If I copy the xyz/rubbellos/rubbelbild_png.php to browser it is OK.
My approach is to bring the request of rubbellos.png to the .php-file. But I get a file not found.
Thx for any hint in advance.
You have 2 errors:
use a RewriteBase since you're talking about a subdirectory /xyz/ in your question.
disable MultiViews option to avoid unexpected behaviour with rubbellos (virtual file and existing directory).
You can replace your current code by this one in your htaccess (which has to be in /xyz/ folder)
Options -MultiViews
RewriteEngine On
RewriteBase /xyz/
RewriteRule ^rubbellos\.png$ rubbellos/rubbelbild_png.php [L]
RewriteRule ^rubbellos\.css$ rubbellos/rubbellos_css.php [L]
Note: don't forget to replace xyz by your real subdirectory's name

Explanation of this .htaccess file

I have recently added a .htaccess file to my website root in order to achieve something I have already asked in this question:
Create blog post links similar to a folder structure
Now, the top answer was brilliant and it worked on localhost, however on my server it doesn't.
To be honest with you, I am not quite sure what these statements in the .htaccess mean exactly. I would be grateful if somebody could explain them line by line.
So this is what I have
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_rewrite.c> # If the module for rewrite is available
RewriteEngine On # turn it on
RewriteBase / # set / to the relative root path
RewriteRule ^index\.php$ - [L] # don't rewrite index.php and stop the rest of the rules
RewriteCond %{REQUEST_FILENAME} !-f # if file does not exist
RewriteCond %{REQUEST_FILENAME} !-d # and file is not a directory
RewriteRule . /index.php [L] # show the index page instead
</IfModule> # close the IfModule clause
I suspect if it doesn't work on your server, that the root directory isn't set properly for the domain or server configuration. You'll wanna look into where the files are placed and how the httpd.conf is configured to use those paths.
Also, for next time Apache has a wonderful documentation on this module that should cover everything I've just said, in more depth.
The actual problem was that the rewrite was not enabled on my virtualhost.
So I enabled it in my virtualhosts and everything works now.
<VirtualHost *>
ServerName mysite.com
DocumentRoot /var/www/html/mysite.com
<Directory /var/www/html/mysite.com>
Options FollowSymLinks
AllowOverride all
</Directory>
</VirtualHost>

mod_rewrite is not working with my urls

I am trying to make SEO friendly URLs of using apache mod_rewrite.
My normal URLs is something like this -
index.php?p=about
index.php?p=contact
index.php?p=this
My expecting SEO friendly URLs should be something similar to this -
localhost/php_advance/ch02/about
localhost/php_advance/ch02/contact
localhost/php_advance/ch02/this
I tried it with creating a .htaccess file and doing some changes to my apache httpd.conf file.
This is my .htaccess file
<ifModule mod_rewrite.c>
# Turn on the engine:
RewriteEngine on
# Set the base to this directory:
RewriteBase /php_advance/ch02/
# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
</ifModule>
And also, At the end of the httpd.conf file, I added some code like this -
<Directory "C:/wamp/www/php_advance/ch02">
AllowOverride All
</Directory>
NOTE: I am using WAMP server and Windows 07
But this coding is not working for me. Hope someone will help me out.
Thank you.
Make sure mod_rewrite is turned on. In your httpd.conf file, you should have something similar to:
LoadModule rewrite_module modules/mod_rewrite.so
Then make sure your htaccess file is in your /ch02/ directory.
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /php_advance/ch02/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /php_advance/ch02/index.php [L]
</IfModule>

Categories