Is it possible to alias a filename on an apache webserver? - apache

I would like to make e.g. www.address.com/u.exe equal to www.address.com/serverfile.php or pl?
Is it possible?
So if someone types www.address.com/u.exe should get servefile.php...
Thanks for showing the right direction..
This seems to work. RewriteEngine on also had to be added.
I had to change .htaccess file
RewriteEngine on
RewriteRule ^u\.exe$ serverfile.php

Yes. That's what the mod_alias Apache module does for you: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#alias

Yes, it's possible with mod_rewrite like below.
RewriteRule ^/u.exe$ /serverfile.php [L]
Or below if you want to display serverfile.php (via a redirect).
RewriteRule ^/u.exe$ /serverfile.php [RL]

Related

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

Using mod_rewrite correctly on Debian server

the good old mod_rewrite. I can't seem to get it right.
Typical scenario: A user types in "http://domain.com/page"
I want that the user is being redirected to "http://domain.com/page/page2"
My htaccess file looks as follows:
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^/page/$ page/page2
RewriteRule ^/bla/$ page/page2/bla
The first rewrite rule works, the second on the other hand doesn't seem to have any effect. Any idea? Maybe a better way to do this?
And another question:
As I said the first rewrite works just fine, but the url is not pretty. "http://domain.com/page" changes to "http://domain.com/page/page2". Is there a way to keep the typed in url but still forward the user to the actual link?
I presume the .htaccess is in your DocumentRoot.
How does your /bla containing look like? This should not rewrite the URL in the browser.
Use this:
RewriteEngine on
RewriteBase /
RewriteRule ^(/?)page/?$ $1page/page2 [L]
RewriteRule ^(/?)bla/?$ $1page/page2/bla [L]

Rewriting url using mod-rewrite and apache

I'm trying to add some rewriting on my site, but it seems to not work, I'm using apache and .htaccess.
The code in my .htaccess file is:
RewriteEngine On
RewriteRule ^/?os_framework/?$ /os_framework/index.php?module=home [L,NC,QSA,PT]
This should send http://localhost/os_framework/ to http://localhost/os_framework/index.php?module=home
But it seems not to.
Any help would be appreciated.
In advance, thanks
Edit: Fixed the above, shouldn't have the os_framework/ in the search pattern, however now i cant get this one to work:
RewriteRule ^/(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
And what is wrong with
RewriteRule ^(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
Why does that throw a error 500? it should work
Try this:
RewriteEngine On
RewriteRule ^os_framework/?$ os_framework/index.php?module=home [L,NC,QSA]

.htaccess help, replacing URI segment

I have a URL that looks like this...
http://www.domain/a/var
I am hoping it would be possible to rewrite the URL using .htaccess so that /a/ would be replaced by /b/.
Is that possible, and if so, how?
RewriteEngine On
RewriteRule ^a/(.*) /b/$1
This should do that you need
RewriteEngine on
RewriteRule ^/a/(.*) /b/$1
Note: You'll need to add [PT] or similar if you need mod_alias or mod_index or similar to work using the new path.

Apache rewrite url

I am trying to rewrite url and it fails. May I know what is wrong? Would someone please enlighten me? I placed the code in .htaccess. I have enabled rewrite_module too.
RewriteEngine On
RewriteRule /place/^([a-zA-Z0-9])$ /placelink.php?lid=$1
For example: domain.com/place/xyz -> domain.com/placelink.php?id=xyz
Update:
I have just found out that my syntax is now correct. But it is not mod_rewrite that is not working. phpinfo shows mod_rewrite module is available.
Update 2
RewriteEngine On
RewriteRule ^/?test\.html$ test.php [L]
Chances are you want this...
RewriteEngine On
RewriteRule ^place/([a-zA-Z0-9-]+)/?$ /placelink.php?lid=$1
This will take requests for..
domain.com/place/the-moon
...and will serve up...
domain.com/placelink.php?lid=the-moon
^ means 'the start of the string. /path/ is a literal. So you're asking for a string which has /path/ in it, after which the string starts. This is logically impossible. See http://regularexpressions.info for more information about regexes.