.htaccess rewrite frustrations - ruby-on-rails

I made my website in PHP originally and used parameters for page/product identification.
ex.) http://mysite.com/?pid=my_products&sid=golf_clubs&GTIN=111111111111
I've now moved over to using RoR and I'd like to set up redirects to clean up the urls
http://mysite.com/my_products/golf_clubs/111111111111
I've looked at tutorials and arrived at this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ ?pid=$1&sid=$2&GTIN=$3 [NC]
I've tested a url and it doesn't redirect at all. I've tried putting this in the .htaccess file to debug and chmodded the log folder to 775 but I get a 500 error:
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
As far as rewrite statements in .htaccess goes, this is what I have:
RewriteEngine On
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
RewriteBase /
RewriteCond %{REQUEST_URI} ^/downloads.*
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ pid=$1&sid=$2&GTIN=$3 [NC]
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
* I am on a shared hosting plan with Bluehost (good gawd I wish they'd update to Rails 3 soon)

If I understand you correctly, you have your rule backwards. You want to catch your old URLs and rewrite them to the new format, correct? In that case, you want this:
RewriteRule ^\?pid=(\w+)\&sid=(\w+)\&GTIN=(\w+) /$1/$2/$3 [NC,R=301,L]
Adding a 301 status will tell the user agent (including well-behaved search bots) that your canonical URLs have permanently changed.

Related

Removing .html and .php from links in IIS

As the title says, I would like to know how to remove ".html" and ".php" from links in a Windows server (IIS).
Most of the information I have found about this subject was only valid for Linux servers.
Only today I heard about the file web.config and got new material to study. Nevertheless, I am still struggling with it.
You can use the url rewrite module for IIS: http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module
Here is a good tutorial on the subject: http://www.surfingsuccess.com/asp/iis-url-rewrite.html
Read about friendly-url and .htaccess
See an example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/?$ index.php?pag=$1 [NC]
RewriteRule ^([a-z0-9-]+)/([0-9]+)/?$ index.php?pag=$1&id=$2 [NC]
</IfModule>
That way when you use:
localhost/contact
The server will do an internal redirect to:
localhost/index.php?pag=contact

Zend Framework 2 without Vhost configuration

I had finished my first web application using Zend Framework 2 and I'm about to put it online.
But may web host doesn't allow me to change my vhost configuration!
The .htaccess file is allowed.
So my question is: How to set up my ZF2 app with only .htaccess files?
Assuming you have a standard ZF2 application based of the skeleton, then try creating a .htaccess file in the root with this in it:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]
I haven't tested this, but in theory, this should effectively move the document root to the public directory while keeping it out of the url.

htacces redirection

I would like to remove the filename from a "subdirectory" URL and just display the subdirectory in the browser URL.
So:
www.domain.com.au/login/public_index.php
becomes:
www.domain.com.au/login/
I have tried various solutions from this site and the internet, but none have worked.
If possible I would like to put the new redirection in the root htaccess file?? and not in the subdirectory htaccess file?
* Just in case other redirections in the htaccess file are in conflict with any new subdirectory redirections, here are the other (and necessary) redirections in the htaccess file that perform important jobs:
#(1) A redirection to remove root level index.php from the url
# Redirect index.php to domain.com.au
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com.au/ [R=301,L]
#(2)
# Redirect domain.com.au to www.domain.com,au
RewriteCond %{HTTP_HOST} ^domain.com.au [NC]
RewriteRule ^(.*)$ http://domain.com.au/$1 [L,R=301]
#(3)
# Force SSL on login directory
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} login
RewriteRule ^(.*)$ https://www.domain.com.au/$1 [R,L]
Any help would be appreciated as all solutions to add the new subdirectory URL redirect thus far have failed.
Regards,
Peter
You can do something similar to what you did in #(1):
#(4)
# remove public_index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)/public_index\.php\ HTTP/
RewriteRule public_index\.php$ http://www.domain.com.au/%1/ [R=301,L]
# internally add it back
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1/public_index.php -f
RewriteRule ^ /%1/public_index.php [L]
That's for any subdirectory, but if you're only interested in login, that gets easier:
#(4)
# remove public_index.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /login/public_index\.php\ HTTP/
RewriteRule public_index\.php$ https://www.domain.com.au/login/ [R=301,L]
RewriteRule ^/?login/?$ /login/public_index.php [L]

Symfony 1.4 Error 400 on the home page in prod env (not in dev env)

I have made an tested an application on my computer and all is working fine (I used only the dev environment via the frontend_dev.php page).
When I deploy this application on the test server, I have the error 400 Bad Request below:
Bad Request
Your browser sent a request that this server could not understand.
I accessed to the website with the normal URL.
I'm using symfony 1.4 with doctrine (no others plugins are involved), wamp 2 (php 5.3, apache 2.0.59).
If I try to access the application with the dev environment, it works.
Try checking if the no_script property on your application setting.yml file. If should be set to False, clear cache and try again
Thank you very much Guiman. You have leaded me to the answer.
The no_script_name property wasn't the origin of the problem. Instead, I have my .htaccess in the web directory which was badly generated (I swear on my pet's head that I haven't edited this file before). Below is the file generated by symfony (the bold attribute isn't working within the code one, look at the last line):
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
And this is the updated one (again, look at the last line):
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ **/myApp/index.php** [QSA,L]
</IfModule>
I don't know what happened but I will check on the symfony website I there are similar issues.

How do I edit .htaccess to allow both rails and wordpress requests?

I want to run an instance of wordpress within my rails app. I currently have wordpress files housed in public/wordpress, but I need to configure my .htaccess file to allow both types of requests. How do I do that? currently, .htaccess is:
General Apache options
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)/!$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 "Application error Application failed to start properly"
You should start by adding this to the .htaccess in your public folder:
RewriteCond %{REQUEST_URI} ^/wordpress.*
RewriteRule .* - [L]
However, this is not the whole story. You also need to edit /etc/apache2/sites-available/
with this addition (to tell Rails not to process anything in /blog as part of the app):
<Location /wordpress>
PassengerEnabled off
Also in /etc/apache2/apache2.conf you may need to tell Apache to make any directory index (e.g. wordpress/) execute an index.php file if there is one:
DirectoryIndex index.php

Resources