mod_rewrite usage - php

I have situation that I need to direct all request matching http://mywebsite.com/portfolio/[anyname] to http://mywebsite.com/portfolio.php?username=[anyname]. Can anybody help me write .htaccess file for that.
Thanks

You can likely get away with a simple rule like:
RewriteEngine On
RewriteRule ^portfolio/(\w+)$ portfolio.php?username=$1 [L]
If the usernames can contain anything else but letters, use .+ instead of \w+
The other options are documented under http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Related

Regular expression to extract target url from a htaccess rewrite rule string in php?

I need to check our htaccess rewrite rules if the target url is still ok. For this task I want to be able extract the target part from any rewrite rule. Actually what I need is an appropriate regular expression pattern.
Typical htaccess rewrite rules may look like this:
RewriteRule ^media/content/some_source.pdf$ / [R=301,L]
RewriteRule ^another_source/with_subfolder([-0-9a-zA-Z\w]*) landingpages/some_folder/target.php [L]
The fat part needs to be extracted! Hope somebody is more familiar with regex to help me solve this issue.
Thanks
explode on ' ' space and then the 3rd item (index 2) will do the job just fine

Apache RewriteRule rule

Alright, I give up. I just can't quite wrap my mind around apache rewrites, I've looked through a lot of the stackoverflow suggestions and none seems to make sense to me.
So, I have a script that current renders content based on www.example.com/index.php?article=some-article-name
But, I want the user to think that page is www.example.com/section/some-article-name
I've tried using stuff like
# Turn on URL rewriting
RewriteEngine On
RewriteRule ^/section/([a-zA-Z0-9\-]+)$ index.php?article=$1
I discovered the answer thanks to the direction of all of these folks.
RewriteRule ^section/([a-zA-Z0-9]+)$ test.php?article=$1
RewriteRule ^section/([a-zA-Z0-9]+)/$ test.php?article=$1
You need both to handle 2 different types of requests, ones with a / at the end and those that don't.
You may want a simpler rule like.
RewriteRule ^/section/(.*) index.php?article=$1
A name like some-article-name will fail because you won't match the hyphen. If you want a limited regex try something like:
RewriteRule ^/section/([-_.a-zA-Z0-9]+)$ index.php?article=$1
This will match ASCII alphanumeric characters along with punctuation most likley to be in the name.
Either of these rules will fail if you have parameters on the incoming request.
Your RegEx is SO close. You have a capturing group ([a-zA-Z0-9]+) that is looking for one or more lower case letters, upper case letters and/or numbers, but what it isn't looking for is a hyphen. Try this:
RewriteRule ^/section/([a-zA-Z0-9\-]+)$ index.php?article=$1
Because the hyphen has special significance in Regular Expressions you need to escape it.

Very simple RewriteRule doesn't work

I have index.php with only one line in it
<?php echo $_GET['a']; ?>
And also I have a .htaccess with only two lines in it
RewriteEngine on
RewriteRule ^([a-z]+)$ ?a=$1
Why when I enter /ab.cd I get a 404 error, but if I entering /abcd. it doesn't give me a 404 error and PHP echos abcd (without the dot on the end of the url).
Can anybody tell me why my rewrite rule allows abcd. through instead of giving me a 404 error?
The regular expression you've used, ([a-z]+), matches only the lowercase letters "a" through "z". It will never match a series of letters followed by a period followed by a series of letters, like your attempt with ab.cd.
If you want to match periods as well, you need ([a-z.]+).
EDIT:
I've just tested your Rewriterule (standalone) in my dev environment and it works. It doesn't allow /abcd. through on mine - It gives me a 404 error, so there must be something somewhere in your environment that's affecting your rules.
-initial post-
Looks like the period(.) in your /ab.cd isn't defined in the Rewriterule - just lowercase a-z chars.
Use:
RewriteEngine on
RewriteRule ^([a-z.]+)$ ?a=$1
^
I just rebuilt the situation on my server and I'm getting the desired 404 error. I only added the [L] flag to the RewriteRule.
.htaccess
RewriteEngine on
RewriteRule ^([a-z]+)$ test.php?a=$1 [L]
test.php
<?php echo $_GET['a']; ?>
Are there maybe any other rewrite rules following that might interfere with your request? Even in other files that get included? In that case, the L flag might help you.

Making wordpress like url by mod-rewrite

I am trying to edit the URL http://example.com/paper-ads-details.php?req=43674&rnd=1308632400 into http://example.com/ads/43674/1308632400 by following rewrite_rule
RewriteRule ^ads/([0-9]*)/([0-9]*)$ paper-ads-details.php?ads&req=$1&rnd=$2
But it's not working. I want to just hide the parameters. Any other suggestion will be appreciated. Thanks.
Both your rewrite rules make no sense. There are no $1 or $2 and what you are trying to do is impossible as no webserver can guess what the req and rnd parameters were.
You may be interested in rewriting http://example.com/ads/43674/1308632400 into http://example.com/paper-ads-details.php?req=43674&rnd=1308632400. Here is the code:
RewriteEngine On
RewriteRule ^ads/([0-9]*)/([0-9]*)$ paper-ads-details.php?req=$1&rnd=$2
If you want to rewrite: http://example.com/paper-ads-details.php?req=43674&rnd=1308632400 into http://example.com/ads?req=43674&rnd=1308632400, then try this:
RewriteCond %{QUERY_STRING} ^req
RewriteRule paper-ads-details.php?(.*)$ http://example.com/ads?$1 [L]
You can validate your rewrite-rule here - http://martinmelin.se/rewrite-rule-tester/
The question is quite confusing, made more so by the sample you provided, but if I understand correctly that you want the URL to contain no more parameters, then you won't have any $1 or $2 to work with. These would be the parameters that you match with your regular expression, but your regular expression doesn't match any parameter.
So, if you want /ads to go to /paper-ads-details.php?req=43674&rnd=1308632400, you can simply write a rule that does exactly that:
RewriteRule ^ads$ /paper-ads-details.php?req=43674&rnd=1308632400 [L]
The above rule will match the URL /ads and will call /paper-ads-details.php?req=43674&rnd=1308632400 every time that URL is requested.

What is wrong with this URL rewrite?

I want (for example)
discuss/1/title/where-are-you
to output
discussPage.php?id=1&title=where-are-you
I have the following commands in a .htaccess file which is in the same directory as discussPage.php:
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: discuss/ID/title/TITLE
# Output: discussPage.php?tid=NAME&title=TITLE
RewriteRule ^discuss/(\w+)/(\w+)/(\w+)/?$ discussPage.php?id=$1&title=$3
Right now I am getting a 404 error when I try to visit a page like discuss/1/title/where-are-you even though discussPage.php?id=1&title=where-are-you works fine.
P.S. mod_rewrite is enabled (i have used it for other purposes).
Your problem is that \w does not match the "-" in the last bit.
You can simply allow dashes in there using a broader character class like [\w-]+. I would be careful with solutions like [^/] because they allow anything except a slash, which is quite permissive. White-lists are always safer and avoid surprises.
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: discuss/ID/title/TITLE
# Output: discussPage.php?tid=NAME&title=TITLE
RewriteRule ^discuss/(\w+)/(\w+)/([\w-]+)/?$ discussPage.php?id=$1&title=$3
Note that the - has to be last in the character class, otherwise you should escape it, because if it's in between two other characters it behaves as a range, like in [a-z].
To debug, you can always echo $_SERVER['QUERY_STRING'];
But I believe the correct code would be something like this.
RewriteEngine on
RewriteBase /
RewriteRule ^discuss/([0-9]+)/title/([a-z0-9\-]+)?$ discussPage.php?id=$1&title=$2 [NC,QSA]
You might want to fix your RewriteBase value, though.
Also - as seen in the last condition, you can only have alphanumeric characters and hyphens in the where-are-you part. (but something tells me you won't be needing other characters!)
Works for me:
RewriteEngine On
RewriteBase /
RewriteRule ^discuss/([^/]+)/([^/]+)/([^/]+)/?$ discussPage.php?id=$1&title=$3 [R]

Categories