I have this url:
http://www.site.com/en/about.php?id=112&name=andrew marshall dickens
and i would like to rewrite it like this:
http://www.site.com/112/andrew-marshall-dickens.html
so far:
RewriteRule ^([^/]*)/([^/]*)\.html$ /en/about.php?id=$1&name=$2 [L]
I'm having trouble with the '-' character.Any suggestions ? Thanks!
Well you're attempting to use a Regex to remove characters from the middle of a string which could have any number of that character in it in the middle of a RewriteRule. On one hand that's not really possible, on the other hand, you're passing the ID in, so I assume you can get the name using the id in your PHP script, so there's not really a need to parse the name from the URL variables, and as a 3rd option, why not just str_replace the - characters in PHP and ucwords() the string before outputting it if you want to use the name variable?
I believe you don't need to pass name param because id can get that.
Anyway:
RewriteRule ^([0-9]+)/([a-z-]+)\.html$ /en/about.php?id=$1&name=$2 [L]
But hey, reading the comment, i just realized: what's your problem? Your regex should already work
Related
I'm building this real estate script using PHP and I want the listing page url's to be like /listing/this-is-the-title-436. This url is generated in PHP and the last part of the url, after the last instance of ' - ' is the listing id. But I cannot find a way to find the last instance of a dash and use the rest as a variable in .htaccess.
Note that the title can have any amount of spaces therefore any amount of dashes but the listing id will always be at the end, after the last dash.
To summarize, I want urls like /listing/this-is-the-title-436 to redirect to /assets/inc/listing.php?listing=436 with .htaccess.
Any help would be appreciated, thanks!
The easiest way is to test a numerical value at the end:
RewriteEngine on
RewriteRule ^listing/.+-(\d+)$ /assets/inc/listing.php?listing=$1 [L,QSA]
But if you're not just using numeric values, you can also test for the absence of - in the last part:
RewriteRule ^listing/.+?-([^-]+)$ /assets/inc/listing.php?listing=$1 [L,QSA]
So this may sound weird, however I currently have mod_rewrite set-up to pass 2 variables through.
RewriteRule ^profiel/(.*)$ index.php?p=profiel&user=$1
In the second var (&user=), it passes a username which is retrieved through GET in PHP. However, some of the usernames can have question marks in them. However if this is the case, the question mark won't be passed to the GET variable. (For example: "www.example.com/profiel/whoami?" ends up as just "whoami" instead of "whoami?")
I honestly don't know how to solve this problem. Any help would be great!
You can use this rule by capturing your values directly from THE_REQUEST variable:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(profiel)/(\S+)\s [NC]
RewriteRule ^profiel/ index.php?p=%1&user=%2 [L,NC]
You can use the PHP function urlencode to encode the username. For example, whoami? will become whoami%3F. So your url will become www.example.com/profiel/whoami%3F
Then to retrieve your username, you can use urldecode.
Here's the documentation on both function:
urlencode
urldecode
Consider the following scenario:
I want to be able to access http://www.example.com/word/hello/, where the word hello is variable. So I set up .htaccess to configure that.
RewriteEngine On
RewriteRule ^word/(.+)/?$ displayword.php?word=$1 [L]
I used .+ because I also want to filter any symbols such as ?+-.!;: etc.
And I set up my PHP file accordingly:
<?php
echo $_GET['word'];
?>
Remember that this is just a scenario. Now, I went to this URL: http://www.example.com/word/Are you ok?/, and the page outputted this:
Are you ok
And I couldn't figure out why. But then I realised that the question mark symbol is the starting point of the URL variables.
So is there a way to 'url encode' the question mark in the above example, in order for it to be displayed correctly?
There is no need to encode it, try this:
RewriteEngine On
RewriteRule ^word/([a-zA-Z0-9-=_.?]+)/?$ displayword.php?word=$1 [L]
It will display ? in the parameter and any other character you add to the [group]. I did not test if the rule works, though, but I suppose it does. Looks ok and that is not the question.
I don't know heaps about .htaccess files, but you could change your PHP script to use $_SERVER['PATH_INFO'] instead of $_GET or $_REQUEST.
Particularly, this comment might help you out.
In the HTTP protocol the "?" separates the querystring from the rest of the URL, so I don't think it will be possible to use it directly inside the URL. One solution would be to encode the question mark into %3F.
Then you can use string urldecode (string $str) to decode the string.
See this URL Encoding Reference for the encoding of other characters.
Change your code to this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+word/([^/]+) [NC]
RewriteRule ^ index.php?word=%1 [L,QSA]
Reason this works is because RewriteRule works on %{REQUEST_URI} which gets URI i.e. string before question mark ? however %{THE_REQUEST} works on the full URL that includes question mark ? as well.
I'm having a brain cramp. I'm using htaccess to rewrite a page and sometimes the variable that gets passed through will have a / (forward slash) in the variable. Sometimes there will be a slash and sometimes there won't but it is super important that all of this is treated as one variable. I'd really rather not reprogram all my pages with a str_replace() to switch a - for a / and then make a call to a database. For example:
http://www.example.com/accounting/finance.htm
Accounting/Finance is one variable that I need.....it is not in an accounting directory and then there's a page called finance.htm in accounting. So far I've got something like
RewriteRule ^([A-Za-z]+.*[A-Za-z]*)\.htm$ mypage.php?page=$1 [L,NC]
But it doesn't like it.
Can someone help me out?
Thanks in advance.
REPLY TO COMMENTS/ANSWERS
The specific rule that I'm looking for is something like this.....
[start of string]...1 or more letters...[possibility of a / followed by 1 or more letters].htm[end of string]
The two answers given below aren't working...I'm pretty sure it keeps treating it as a directory and not an actual "filename". As soon as I remove the forward slash the page works just fine...
If i get you right, you just need this one:
([A-Za-z/]*)\.htm
it should work with every combination of / or not-/
e.g.
accounting/finance.htm
test.htm
A slash is just another character. Apart from that, your regexp looks unnecessarily complex. For instance, .*[A-Za-z]* is not different from .* and also [A-Za-z] can be shortened to [a-z] if you use the [NC] flag.
Your precise rules are not entirely clear, but you probably want something on this line:
RewriteRule ^([a-z/]+)\.htm mypage.php?page=$1
I'm building a PHP application using CodeIgniter. It is similar to Let Me Google That For You where you write a sentence into a text input box, click submit, and you are taken to a URL that displays the result. I wanted the URL to be human-editable, and relatively simple. I've gotten around the CodeIgniter URL routing, so right now my URLs can look something like this:
http://website.com/?q=this+is+a+normal+url
The problem right now is when the sentence contains a special character like a question mark, or a backslash. Both of these mess with my current .htaccess rewrite rules, and it happens even when the character is encoded.
http://website.com/?q=this+is+a+normal+url? OR
http://website.com/?q=this+is+a+normal+url%3F
What does work is double-encoding. For example, if I take the question mark, and encode it to %253F (where the ? is encoded to %3F and the % sign is encoded to %25). This url works properly.
http://website.com/?q=this+is+a+normal+url%253F
Does anyone have an idea of what I can do here? Is there a clever way I could double encode the input? Can I write a .htaccess rewrite rule to get around this? I'm at a loss here. Here are the rewrite rules I'm currently using for everyone.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /index.php/app/create/%{QUERY_STRING}? [L]
Note: The way CodeIgniter works is they have a index/application/function/parameter URL setup. I'm feeding the function the full query string right now.
If your’re using Apache 2.2 and later, you can use the B flag to force the backreference to be escaped:
RewriteCond %{QUERY_STRING} ^q=.*
RewriteRule ^ /index.php/app/create/%0? [L,B]
I usually do human readable urls like this
$humanReadableUrl= implode("_",preg_split('/\W+/', trim($input), -1, PREG_SPLIT_NO_EMPTY));
It will remove any non-word characters and will add underscores beetween words