htaccess hellhole of the universe - .htaccess

Welcome to my doom.
My problem so far is something of a mystery.
Rules that are working perfectly on my website online, but not on my localhost. The rules are rather simple, but I can't find the trick to solve the bloody nightmare.
I have an url like this one http://frostpath.lx/access/articles
It's a Virtual Host on WAMP localhost with this conf.
<VirtualHost frostpath.lx>
ServerName frostpath.lx
ServerAlias frostpath.lx
DocumentRoot "E:/Projects/frostpath"
<Directory "E:/Projects/frostpath/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
So far so good that seems to work, the site is on and running, css load, scripts, everything's so good. mod_rewrite is on, I checked and rechecked with different scripts found online.
But the beast ain't satified. .htaccess (I think the problems comes from there but who knows - I might have been focusing on the wrong thing for hours)
RewriteEngine on
RewriteBase /
RewriteRule ^(access)$ access.php [NC,L]
RewriteRule ^(access)\/([^\.]+)$ access.php?$2 [NC,L]
RewriteRule ^(access)\/([^\.]+)\/([^\.]+)$ access.php?$2&start=$3 [NC,L]
Basically,
http://frostpath.lx/access/articles this is only redirecting to access.php and doesn't show the articles.
http://frostpath.lx/access?articles this one works perfectly.
But I don't want a "?" I want a "/". Abide by the rules .htaccess!
I tried stuffs but no luck so far
/access.php?$2
/access?$2
/?access.php&$2
And everything above with and without slashes. Honestly I ask here because I'm running out of ideas.
Also, rules are working on my index.php but not on access.php which is another controller. Mindblown.
So what the hell is wrong with my rules?
Help me please, despair is slowly hurting my keyboard.

You can use:
RewriteEngine on
RewriteBase /
RewriteRule ^access$ access.php [NC,L]
RewriteRule ^access/([^.]+)/([^.]+)$ access.php?$1&start=$2 [NC,L]
RewriteRule ^access/([^.]+)$ access.php?$1 [NC,L]
With article rule before.
Or change your 2nd rule:
RewriteRule ^access/([^./]+)$ access.php?$1 [NC,L]
Because for now it also applies to the 3rd case, as / is included in the first group

Related

Symfony .htaccess rewrite and redirect not working

I know very little about symfony framework. I copied a website www.example.net to www.example.com site works with url www.example.com/www/app.php or even with www.example.com/www/ but I want it to redirect automatically to app.php without showing it in the url. My htaccess in the www (web) directory is as follows:
&ltIfModule mod_rewrite.c&gt
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
&ltIfModule&gt
It doesn't redirect.
Thanks
In your Vhost:
Change this line (Directory section):
AllowOverride None
to:
AllowOverride All
Instead of that rule can you try:
RewriteBase /www/
RewriteRule ^((?!app\.php).*)$ app.php/$1 [NC,L]
Sorry I can't test with Symphony as I am not near my home computer.
A bit late to this game but I just found an Apache directive that saves so much time and makes your htaccess cleaner. The only caveat is you must have Apache 2.2.16+. To have all urls except valid files (images, etc) use app.php as a front controller use the FallbackResource directive.
<Directory "/web/symfony-project">
FallbackResource /app.php
</Directory>

htaccess A quick move, from pagefor.local/USERNAME to pagefor/USERNAME.local

Morning all,
I have spent Sunday attempting to rewrite a domain to no avail. I'm using MAMP and have changed my Hosts folder and MAMP httpd.conf with the following :
Addition to hosts file:
127.0.0.1 apagefor.local
Addition to httpd.conf file:
<VirtualHost *>
ServerName pagefor.local
DocumentRoot /Applications/MAMP/htdocs/pagefor
</VirtualHost>
This allowed me get rid of the pesky localhost:8888/pagefor/ to just using a nice local url of pagefor.local this all works perfectly. But now im trying to attempt to do a htaccess rewrite if a username is placed directly after the domain. Allowing me to do the following:
Change this:
pagefor.local/LEE
to this:
pagefor/LEE.local
So what have I tired? Well I thought I could do something like the following (within .htaccess):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^`pagefor.local` [NC]
RewriteRule ^pagefor.local/([A-Za-z0-9-.]+) `http://pagefor/$1.local`
Im still trying to workout how to do this, if anyone can tell me where im going... And if im on even on the right path (pun intended :) ) Thank you.
Update
I have managed to get the url to change from http://pagefor.local/lee to http://pagefor/lee.local Thanks to anubhava. The only contention I have is I originally have everything go to my index.php file which controls the entire application... So would it be possible to do the following:
RewriteCond %{HTTP_HOST} ^pagefor\.local$ [NC]
RewriteRule ^([A-Za-z0-9-.]+)$ http://pagefor/$1.local [NC,L]
Then Have the following line to get $1 into my index.php:
RewriteRule ^([A-Za-z0-9-.]+).local$ index.php?url=$1 [NC,L]
Therefore when the Url is pagefor/lee.local its on the following index.php?url=lee. I tired the above but it did not succeed. I hope im on the right track to solving this problem.
Try this instead:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^pagefor\.local$ [NC]
RewriteRule ^([A-Za-z0-9-.]+)$ http://pagefor/$1.local [L,R]

Can we use httpd.conf instead of .htaccess for clean URL?

I have read that setting changes in httpd.conf are much better as compared to making changes in .htaccess as the later one is parsed at Runtime while .conf is parsed at the time of starting Apache. So, is it possible to have all the .htaccess functionality in httpd.conf or there are a few things that have to done in .htaccess only.
Also, can you suggest how to debug clean URL issues?
Basically, I am not able to get the clean URL working. I can access the show.php file but am not able to access the GET variables. Here is the .conf settings.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/show/([^/\.]+)/([^/\.]+) http://www.example.com/show.php?id=$1&img=$2 [L,R]
I have also added
Options Indexes MultiViews FollowSymlinks
AllowOverride All
in <Directory "/var/www/html">
Thanks.
As stated in the Apache manual on .htaccess, anything you can put in an .htaccess file can be put inside a <Directory> block within httpd.conf for better efficiency.
As for your issues with rewriting the URL, your first RewriteCond appears to only be matching URLs that aren't www.example.com (the ! in front is a negation modifier). Remove the ! and see if that fixes the issue. (You probably should remove the second RewriteCond as well, until you have the first one matching; otherwise, debugging problems will be more difficult.)

htaccess redirect root to www

This question seems to be asked a lot before, but I'll ask it again, because it doesn't seem specific for my problem:
I want the domain.com, to be directed to www.domain.com
So I use this:
RewriteEngine On
Options +Indexes +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
It just doesn't seem to work.
Now I'm hoping and thinking the problem is with my hoster, but they are not particularly responsive. When I go to the root domain I get a server error 500. When I contacted them they said I had to use a .htaccess file, but I think the problem is the HOSTS file, which does actually point to the right location (the root domain A-record points to the right IP, and the www. points to the root domain, so I'm thouroughly confused...)
Try adding this at the top. The extension of index file is dependant on your programming language.
DirectoryIndex index.html index.php
Options +Indexes +FollowSymLinks -MultiViews

.htaccess mask - SEO

Looking to put the following into my htaccess file:
User visits http://mysite.com/test/test/123
User is actually visiting http://mysite.com/test/index.php?q=123
I have seen it done before but I can't figure it out. The user is not supposed to see that they are visiting the second url. This is particularly useful for SEO. Can anyone help me on this one?
Updated Code for Answer:
Options -Multiviews
rewriterule ^test\/([a-zA-Z]+)\/([0-9]+)$ test\/index\.php?id=$3 [L]
*Note: The Options -Multiviews is needed for GoDaddy hosting to enable mod_rewrite.*
Be sure mod_rewrite is on:
RewriteEngine on
RewriteRule ^test/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ test.php?id=$2
Double test
RewriteEngine On
RewriteRule ^test/test/([^/]*)$ /test/index.php?q=$1 [L]
Single test
RewriteEngine On
RewriteRule ^test/([^/]*)$ /test/index.php?q=$1 [L]

Resources