Apache rewrite url - apache

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.

Related

Redirect urls with query string to seo-friendly directory like urls

I know this question is asked by many users, also I gone through many questions to find the solutions as I am unable to understand the code used in htaccess file. So please kindly help me to solve this issue. I just want to convert a link like:
www.abc.com/project.php?proj-cat=some+project+name
to url like:
www.abc.com/project/some+project+name
OR to like:
www.abc.com/project/some-project-name/
I googled and found a website which creates a htaccess rule for you, using this site I got this:
RewriteEngine on
RewriteRule project/proj-cat/(.*)/ project.php?proj-cat=$1 [L]
but this doesn't redirect to the links above.
Please help me to find the solution for this and help me understand how it works. Thank You!
You can use this code in root .htaccess:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+project\.php\?proj-cat=([^\s&]+) [NC]
RewriteRule ^ project/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^project/([^/.]+)/?$ project.php?proj-cat=$1 [L,QSA,NC]
Reference: 1. Apache mod_rewrite Introduction
2. http://askapache.com

Apache2 rewrite strange behaviours

For a newsletter I'm trying to make clean url's for the unsubscribe page
At first I was having this, but it wasn't working:
RewriteEngine On
RewriteBase /
RewriteRule ^/unsubscribe/(.*)$ /unsubscribe.php?email=$1 [NC,L]
Which is very strange in my opinion. So I tried some other methods and ended up with the following:
RewriteEngine On
RewriteBase /
RewriteRule ^/?unsubscribe/?(.*)$ /unsubscribe.php?email=$1 [NC,L]
This, in fact, is working. But it is giving very strange results.
The email parameter value is now:
.php/test
It is driving me nuts because I don't see why it is behaving this way.
Is there anybody who has an idea on what is happening here and how to get it fixed?
I'd rather not end up string replacing the php, there is something wrong here.
How are you my friend ? :)
Could you try this and let me know if it works? Let's figure this out!
RewriteEngine On
RewriteRule ^unsubscribe/([^/.]+)/?$ unsubscribe.php?email=$1 [L]
Your first Rule doesn't work, because the rule gets what's BEHIND the base in the URL. So if the URL is /unsubscribe/my#email, the RewriteBase (/) gets removed, and the RewriteRule will see unsubscribe/my#email without the leading /.
In your second rule, BOTH /-es are made optional by the ?. So unsubscribe.php/test will find the literal unsubscribe, and take everything behind it - .php/test - and put it in the email parameter. Guess you should use /unsubscribe/test, not /unsubscribe.php/test in your browser when testing.
This rule should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/?unsubscribe(?:/(.+))?$ /unsubscribe.php?email=$1 [NC,L,QSA]
Problem was in your regex which was matching the pattern after first application and doing the rewrite twice.
Ok got it worked out.
It seemed like MultiViews (whatever that may be) was enabled by default.
Adding:
Options -MultiViews
To the htaccess (or apache config) fixed the problem.

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

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]

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

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]