In monorail I'm trying to create a url rewriting rule to give friendly urls to article posts. Here's what the urls look like:
http://domain.com/2010/11/29/Winter-snow-warning
And here's the code in global.asax.cs to rewrite the urls:
RoutingModuleEx.Engine.Add(
new PatternRoute("/<year>/<month>/<day>/<title>")
.DefaultForController().Is("post")
.DefaultForAction().Is("show")
.Restrict("year").ValidInteger
.Restrict("month").ValidInteger
.Restrict("day").ValidInteger
);
This works great, however if there is an exclamation point in the url:
http://domain.com/2010/11/29/Winter-snow-warning!!
Then it doesn't match the rewriting rule and errors out, saying the controller "2010" cannot be found. What am I missing here, is this a bug in monorail?
Perhaps the default matching mechanism of Monorail's routing is not accepting exclamation mark, thus the route does not match and the default /controller/action rule is matched instead, failing since no 2010 controller exists.
A quick workaround could be to to restrict the title to the exact expression that fits your needs. e.g.: .Restring("title").ValidRegex("[-_.+!*'() a-zA-Z0-9]+]")
Related
I want to forward this URL
https://demo.example.com/page1?v=105
to
https://www.example.com/page1
It's not working if I set so:
But if I remove the ?v= part, it works:
Is there any way to include the ?v= part in this page rule?
The rule you mentioned works only for that exact URL: https://demo.example.com/page1?v=105. If it has any additional query parameter or even an additional character, eg https://demo.example.com/page1?v=1050, it won't match the rule.
If you need to use complicated URL matches that require specific query strings, you might need to use bulk redirects or a Cloudflare worker.
I have the url: www.example.com/test/here.
I want to re-direct it to: www.example.com/test2/home#here.
I have tried:
RewriteRule /apply$ www.example.com/test2/home#here [R, NE]
But I am currently getting a server error. Sorry I am not very good with htaccess files.
You are getting a server error because of this:
[R, NE]
You can't have spaces in your rewrite flags, it confuses mod_rewrite and makes it think it's an extra parameter, thus making it think you've not closed your flags with a ].
Besides that, you're regex pattern: /apply$ will never match /test/here. Perhaps, you should try ^test/here$ instead.
In the following thread:
How to create user profiles with PHP and MySQL
I have a number of doubts:
In the first answer (by Chacha102) what is the '$1', I understand that it refers to a paramter but which one?
Does not the code in the first answer redirect to another index.php without renaming the url to something like www.facebook.com/username?
UPDATE:
Where does the edit to the url occur?
Doubt 1:
It refers to value of a get parameter user. For instance, if you have index.php?user=Name it refers to "Name".
Doubt 2:
The code wouldn't redirect. It will just rewrite url to www.domain.com/Name. It's equivalent to www.domain.com/index.php?user=Name.
1: The $1 is called the first captured group from the pattern. It refers to the value ?user=john (Read more about capturing groups)
2: In most of the PHP application the main entry (routing) point of http request is index.php. If you enter the url like http://www.example.com/john actually it would be the same as http://www.example.com/index.php?user=john if you have applied the same mod_rewrite rule from the answer.
I have various products with their own set paths. Eg:
electronics/mp3-players/sony-hg122
fitness/devices/gymboss
If want to be able to access URLs in this format. For example:
http://www.mysite.com/fitness/devices/gymboss
http://www.mysite.com/electronics/mp3-players/sony-hg122
My strategy was to override the "init" function of the SiteController in order to catch the paths and then direct it to my own implementation of a render function. However, this doesn't allow me to catch the path.
Am I going about it the wrong way? What would be the correct strategy to do this?
** EDIT **
I figure I have to make use of the URL manager. But how do I dynamically add path formats if they are all custom in a database?
Eskimo's setup is a good solid approach for most Yii systems. However, for yours, I would suggest creating a custom UrlRule to query your database:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes
Note: the URL rules are parsed on every single Yii request, so be careful in there. If you aren't efficient, you can rapidly slow down your site. By default rules are cached (if you have a cache setup), but I don't know if that applies to dynamic DB rules (I would think not).
In your URL manager (protected/config/main.php), Set urlFormat to path (and toptionally set showScriptName to false (this hides the index.php part of the URL))
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
Next, in your rules, you could setup something like:
catalogue/<category_url:.+>/<product_url:.+> => product/view,
So what this does is route and request with a structure like catalogue/electronics/ipods to the ProductController actionView. You can then access the category_url and product_url portions of the URL like so:
$_GET['category_url'];
$_GET['product_url'];
How this rule works is, any URL which starts with the word catalogue (directly after your domain name) which is followed by another word (category_url), and another word (product_url), will be directed to that controller/action.
You will notice that in my example I am preceding the category and product with the word catalogue. Obviously you could replace this with whatever you prefer or leave it out all together. The reason I have put it in is, consider the following URL:
http://mywebsite.com/site/about
If you left out the 'catalogue' portion of the URL and defined your rule only as:
<category_url:.+>/<product_url:.+> => product/view,
the URL Manager would see the site portion of the URL as the category_url value, and the about portion as the product_url. To prevent this you can either have the catalogue protion of the URL, or define rules for the non catalogue pages (ie; define a rule for site/about)
Rules are interpreted top to bottom, and only the first rule is matched. Obviously you can add as many rules as you need for as many different URL structures as you need.
I hope this gets you on the right path, feel free to comment with any questions or clarifications you need
I'm sure this is dead simple, which is why I'm so annoyed...
The problem originated from using translate_routes. This is very simple to use and presents no problem whatsoever. All I want is to be able to have the default locale prefix in the url, optionally.
Right now, I can do:
GET /controller/ and
GET /fr/controlleur/, but trying
GET /en/controller/ fails.
I figured that the en in the last example is not useful, so I want to 'remove' it using redirection. However, all my attempts up till now have failed.
How can I remove that prefix?
match "/en/*path" => redirect("/%{path}")
http://guides.rubyonrails.org/routing.html#redirection
http://guides.rubyonrails.org/routing.html#route-globbing