I'm trying to forward example.com/signup and example.com/signup/ (with trailing slash) to example.com/signup.php
I wrote following to .htaccess and it works for example.com/signup/ but doesn't work without trailing slash... How can I solve the problem?
RewriteEngine On
RewriteRule ^question/(.*)-(.*) /question.php?qid=$1
RewriteRule ^signup/ /signup.php
RewriteEngine On
RewriteRule ^signup/?$ /signup.php
The question mark makes the slash optional, and the dollar sign means end-of-line, so nothing can follow. You can remove the $ if you like.
You could also allow arbitrary query strings at the end of the URL with something like:
RewriteRule ^signup/?(\?.*)?$ /signup.php$1
That would allow you to pass any query parameters on to your PHP script. For example the URL http://www.example.com/signup?destination=/front-page would be redirected to http://www.example.com/signup.php?destination=/front-page.
Make the slash optional with the ? quantifier:
RewriteRule ^signup/?$ /signup.php
But I recommend you to just use one notation and redirect the other one.
Put a question mark after the slash
RewriteRule ^signup/? /signup.php
? = optional single character in regex matches
Related
I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..
The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123
RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/
RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.
Quite simply when you browse to:
localhost/abc/product.php?id=23
The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL
URL Transformation
http://www.yoursite.com/product/123 <- URL that user goes to
http://www.yoursite.com/product.php?id=123 <- Rewritten URL that the server sees
RewriteRule(s) explanined
A rewrite rule is broken down into three parts (not including the RewriteRule part...)
The regex that it matches against the url
The url that it transforms into
Additional options
Given your rule:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]
This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.
Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.
Regex
The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.
Explanation
^product/([^/.]+)/?$
^ => Start of string
product/ => Matches the literal string product/
([^/.]+) => A capture group matching one or more characters up until the next / or .
/?$ => Matches an optional / followed by the end of the string
Given the URL:
http://mysite.com/product/image.jpg
Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.
You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
Try
RewriteRule ^product/([a-zA-Z0-9_]+)/?$ product.php?id=$1 [L]
can someone tell me how to write redirection rule using regex in .htaccess file.
I want
www.example.com/healthcare/practitioner/nikky.23
to be redirectd to
www.example.com/healthcare/practitioner/#/nikky.23
Note that last parameter after '/' i.e nikky.23 can change in different cases.
Please help.
Try with RewriteRule:
RewriteRule (healthcare/practitioner/).* $1
If you want the last parameter to be without further slashes:
RewriteRule (healthcare/practitioner/)[^/]*$ $1
We need to create a rule on htaccess that will redirect a page according to the url. if the path or last part of the url has only letters or numbers, no underscores, dots, slashes or dashes, we want to use that string found and send it as a parameter to another php page that will decide how to use it.
Something like this:
suppose that /^([^\.,\-,\_,\/]+) means all except '.', '-', '_', '/' and spaces
RewriteRule /^([^\.,\-,\_,\/]+)$ handler.php?alias=$1 [L]
where $1 will give us a string with letters and numbers only.
For example:
mysite.com/someone = handler.php?alias=someone
and
mysite.com/styles.css or mysite.com/images/logo.jpg or mysite.com/page_2, etc
will not be redirected.
I am really lost with regular expressions. Any Ideas?
Thanks!
Have you tried this ?
RewriteRule ^([a-zA-Z0-9]+)$ handler.php?alias=$1 [L]
or
RewriteRule ^([a-z0-9]+)$ handler.php?alias=$1 [NC,L]
or
RewriteRule ^[a-z0-9]+$ handler.php?alias=$0 [NC.L]
try
RewriteRule ^.*([^\/\.\-_]+)$ handler.php?alias=$1
[Edit]: If you don't want to match whitespace (that should never be present in URLs, so that shopuldn't even make a difference, but still ^^), do
RewriteRule ^.*(\w+)$ handler.php?alias=$1
I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..
The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123
RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/
RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.
Quite simply when you browse to:
localhost/abc/product.php?id=23
The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL
URL Transformation
http://www.yoursite.com/product/123 <- URL that user goes to
http://www.yoursite.com/product.php?id=123 <- Rewritten URL that the server sees
RewriteRule(s) explanined
A rewrite rule is broken down into three parts (not including the RewriteRule part...)
The regex that it matches against the url
The url that it transforms into
Additional options
Given your rule:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]
This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.
Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.
Regex
The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.
Explanation
^product/([^/.]+)/?$
^ => Start of string
product/ => Matches the literal string product/
([^/.]+) => A capture group matching one or more characters up until the next / or .
/?$ => Matches an optional / followed by the end of the string
Given the URL:
http://mysite.com/product/image.jpg
Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.
You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
Try
RewriteRule ^product/([a-zA-Z0-9_]+)/?$ product.php?id=$1 [L]
I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.