Sending path and GET to index.php - .htaccess

Let me give you an example:
I open up this address using my browser: http://localhost/AAA/BBB/CCC?id=5&foo=bar
I want to write a .htaccess that implicitly converts the link to: http://localhost/index.php?route=/AAA/BBB/CCC&id=5&foo=bar
Whatever the destination link will be, I don't care; I just want it to contain both path, and GET request. Notice that some URL like the one below don't contain any parameter, but must be parsed correctly:
http://localhost/AAA/BBB
will be converted to http://localhost/index.php?route=/AAA/BBB
Thanks in advance

You just have to capture whatever request url is present and append it as "route":
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?route=$1 [L,QSA]
A side note: if you have access to the real server configuration, then prefer that to .htaccess style files. Those files make things more complex, are notoriously error prone and make the server sloooooow!

Related

Why htaccess not working for mobile browser?

I have website (mzadkm.com) try to RewriteRule short url to app.php page .
So if user browse "mzadkm.com/app" will show "mzadkm.com/app.php" page
RewriteRule ^/app /app.php [L,R=301]
It's work on Computer , but on mobile browser give me 404 page
Any ideas
That probably is what you are looking for:
RewriteRule ^/?app /app.php [L]
The documentation clearly says, that the pattern in a RewriteRule get's applied to the relative path of the request if the rule is implemented inside a distributed configuration file. That means you actually want to match the path app and not /app here. Which is why your rule did not get applied. The ^/?app is a variant to accept both path notations, relative and absolut, which means the same rule can get implemented in the central configuration or likewise in a distributed configuration file (".htaccess").
I took the liberty to also remove the external redirection you showed ("R=301") since that most likely is not what you want, according to the phrasing of your question. Instead you want an internal rewrite .
You need to take care however that you do not implement a rewriting loop. Which would result in failing requests and an "internal server error" (http status 500).
One approach would be that:
RewriteEngine on
RewriteRule ^/?app$ /app.php [L]
Here another one:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?app /app.php [L]
Why things looked fine on your computer, but not on a mobile browser is unclear. Since the same rules get applied and the requests look the same there has to be another reason for that. I suspect you looked at a cached result of a previous attempt somewhere. Remember to always use a fresh anonymous browser window when testing. And to check the response you receive back inside your browsers network console.

.htaccess trims #(hash) sign from parameters

Hey everyone I'm trying to build a little MVC-framework from scratch using PHP.
I'm using simple URL-scheme for this purposes contorller/method/params. My htaccess file looks like this:
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
But when I'm trying to receive url string: explorer/add/c# via $_GET['url'] I'm getting explorer/add/c Even if I encode it like this explorer/add/c%23 still get nothing but explorer/add/c%24 ($-symbol) works just fine. As You can see I'm not a good server administrator, so I hope for your help guys. Let me know if you need more info from me and thanks
The fragment is not sent to the server by a client. I.e. # and anything that follows will never be part of the URL your server sees.
From here:
Fragment identifiers have a special role in information retrieval
systems as the primary form of client-side indirect referencing,
allowing an author to specifically identify aspects of an existing
resource that are only indirectly provided by the resource owner. As
such, the fragment identifier is not used in the scheme-specific
processing of a URI; instead, the fragment identifier is separated
from the rest of the URI prior to a dereference, and thus the
identifying information within the fragment itself is dereferenced
solely by the user agent, regardless of the URI scheme.
If you want to redirect a URL including the fragment part, you need to implement the redirection client-side via Javascript.

.htaccess slug url

I'm wondering if anyone will be able to help me, I'm trying to make a site using slug URLs.
At the moment if a user sees the url it is something like
http://www.thedomain.com/artists-single.php?aid=123
but ideally I would like the URL to be
http://www.thedomain.com/artist/artist-name
.
I have in the database a url friendly artist name which I would like to use.
At the moment within my .htaccess file I have the following:
RewriteEngine On
RewriteBase /
RewriteRule ^artists-single\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The site itself is written with PHP.
Thanks.
You can get Apache to access MySql and map "artist-name" to it's corresponding ID, see here (Thanks to #Marc B for the link). However, you could also do something like this though (this is what I personally use),
RewriteRule ^artist/([a-zA-Z0-9]+)/?$ /artist-single.php?artist=$1 [L]
Then in PHP use $_GET['artist'] to get the value, then query that against the database to get the artist's ID. Or you could use the ID, like
RewriteRule ^artist/([0-9]+)/?$ /artist-single.php?aid=$1 [L]
The URL would be like www.example.com/artist/123, which would pass the id to $_GET['aid']
I would recommend that you make use of a very simple Front Controller. This will give you the least headache and should be rather simple looking at your current setup. Have a quick peak at this for more details: http://www.technotaste.com/blog/simple-php-front-controller/
But just to give you a quick starting point, a Front Controller will be "the first part" that gets executed, traditionally this was the "index.php" file. Using this Front Controller you can then have SEO-friendly URLs and not change any core functionally to your other files.

.htaccess Redirect in case of 404

I have this line of .htaccess
This line is used to get images from another server.
RewriteRule ^resources/fabricantes(.*)$ http://mysecondserver.com/arq/pictures/fab$1
than, if I have the url: http://myserver.com/resources/fabricantes/fab_1.jpg
this image will be get from: http://mysecondserver.com/arq/pictures/fab/fab_1.jpg
The Problem:
In some cases, the image doesn't exists on mysecondserver.com, how can I redirect to a "image unavailable" image in this cases?
First think you need to understand that this rule can only work from mysecondserver.com host not from server.com.
On mysecondserver.com place this .htaccess in /arq/pictures/.htaccess:
RewriteEngine On
RewriteBase /arq/pictures/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ no-image.jpg [L]
On mysecondserver.com, you would have .htaccess checking for -f status of the REQUEST_FILENAME. It could send the user back to the first server, if necessary. The alternative is to explicitly list all known failures back on your first server, which is a lot of work for you (even if you have a complete list). There's no way for the first server to know if a file actually exists on the second server.

Yii and Mod Rewrite: redirect to user language translated url

I'm developing a multilanguage web app with Yii.
I applied changes to hide the index.php, changed urlFormat to path and added to the url path a slug with the user language example /it/index.php /en/index.php etc...
The problem now is that I need to redirect automatically to a different url once the user chooses another language. For example:
http://localhost/~antonio/project/it/women
needs to redirect to:
http://localhost/~antonio/project/it/femme
I have been playing with htaccess with no luck at all. Here is the actual code:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteBase /~antonio/project/
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
#My redirection code (tried a good few more to no use apart from this)
RewriteRule ^it/women$ it/femme
I would really appreciate any help on this issue, as it is driving me mad.
Thanks
Edit::
I surrendered with mod_rewrite. I found another solution by adding this code to /layout/main.php:
<?php
$onurl = Yii::app()->getRequest()->requestUri;
if ($onurl == "/~antonio/project/it/women") {
$this->redirect("/~antonio/project/it/femme");
} elseif ($onurl == "/~antonio/project/it/men") {
$this->redirect("/~antonio/project/it/uomme");
}
Rinse and repeat per combination of language/word
This might not work without setting up a proper Virtual Host (so that instead of local urls like http://localhost/~antonio/project/it/women you have nice urls like http://project1.dev). But I would do that anyway, since it makes your dev environment nicer! (Here's a place to start).
Anyway, I would try this: just leave the .htaccess file set like you normally would for "path" style urls, and then just parse a $_GET['lang'] parameter using the UrlManager? So you would have a urlManager setup like this in your config.php:
'urlManager'=>array(
'urlFormat'=>'path', // use path style parameters
'showScriptName'=>false, // get rid of index.php
'rules'=>array(
// this parses out the first chunk of url into the "lang" GET parameter
'<lang:[\w\-]+>/<controller:[\w\-]+>/<action:[\w\-]+>'=>'<controller>/<action>',
)
)
This way a url like this http://project1.dev/it/controller/action will redirect to the "action" action in your Controller like normal (controller/action), but in your action $_GET['lang'] will now have the value of "it".
I hope that helps, it's kind of a shot in the dark. I'm not sure how you are actually triggering the different languages, so a $_GET parameter might not be helpful after all.
Found a way to do this in htaccess:
RewriteCond %{REQUEST_URI} ^/~antonio/project/it/donna/shoes/(.*)$
RewriteRule ^it/donna/shoes/(.*)$ /~antonio/project/it/donna/calzature/$1 [L,R]

Resources