I have a controller I am using for redirects called redirect.php. This controller has a single _remap($method).
This _remap method works by attributing any URI string after the redirect controller back into the controller. For example, a request to http://mysite.com/redirect/sometext would send the request back to http://mysite.com/redirect
This is the behavior I want, but how I would make this happen at the index level so instead of having to type http://mysite.com/redirect/sometext , I could type http://mysite.com/sometext and get the same behavior. This is challenging because Index.php is used by CodeIgniter so I can't put the _remap method here. As, I'm not super familiar with routes and .htaccess any help would be appreciated to get this working.
If you wanted to go the routes way, you would make a route for each controller you have, aka controller somepage would have a route $route["somepage"] = "somepage";, then after all of them (at the very bottom of the routes). Routes work by finding the first matching regex and then using it, so to make sure all of your controllers work, and only catch regular text, you need each of these rules.
Then we make the 'default' rule, this would run for everything that didn't get caught in a previous route check. Something like $route["(.*)"] = 'redirect/index/$1';. This would redirect you to the controller redirect, with the first uri segment (segment 3) being the sometext in your example above. Do note that this will also run for 404 pages, it would run for everything that isn't defined in the above rules.
I don't see a point in this, if I were you I would just send it to the redirect controller, it would make things a bit less messy, but there is of course a way as shown above.
Related
I'm quite new to Node.js and Express (and relatively new to http stuff in general). While going through various tutorials, I got curious about why the paths defined in the routes always have a leading forward slash. You would have something like router.get('/sub_path', handler); which would be mounted using something like app.use('/parent', the_router); and the whole thing would then be accessed by going somewhere like http://localhost:3000/parent/sub_path. I tried omitting the slashes in the calls to get() and use() and sure enough things stopped working correctly.
Is there a technical reason that the slashes are necessary or was it just an arbitrary design decision (perhaps for consistency or clarity or something)? I didn't see any errors in the server, so is there maybe a case where the lack of slashes actually does something useful?
Well, when you have an URL like http://www.somedomain.com/somepath, the path in that URL is /somepath. It's not somepath. It's /somepath. So, if you want to match that above URL with a route, you need to match /somepath.
I supposed that it would have been possible for Express to "assume" every single route string had an implicit "/" on the front of it and not make you type it, but it would be a bit misleading. The route you're asking to match starts with a /.
You are allowed to use a regex to match a route, but that's the only case I've seen where you don't use a leading /.
For further discussion of actual developer motivation at the time of designing/writing the code, you'd have to ask people who actually wrote Express. Not something we can speak to here.
I'm quite new to Node.js and Express (and relatively new to http stuff in general). While going through various tutorials, I got curious about why the paths defined in the routes always have a leading forward slash. You would have something like router.get('/sub_path', handler); which would be mounted using something like app.use('/parent', the_router); and the whole thing would then be accessed by going somewhere like http://localhost:3000/parent/sub_path. I tried omitting the slashes in the calls to get() and use() and sure enough things stopped working correctly.
Is there a technical reason that the slashes are necessary or was it just an arbitrary design decision (perhaps for consistency or clarity or something)? I didn't see any errors in the server, so is there maybe a case where the lack of slashes actually does something useful?
Well, when you have an URL like http://www.somedomain.com/somepath, the path in that URL is /somepath. It's not somepath. It's /somepath. So, if you want to match that above URL with a route, you need to match /somepath.
I supposed that it would have been possible for Express to "assume" every single route string had an implicit "/" on the front of it and not make you type it, but it would be a bit misleading. The route you're asking to match starts with a /.
You are allowed to use a regex to match a route, but that's the only case I've seen where you don't use a leading /.
For further discussion of actual developer motivation at the time of designing/writing the code, you'd have to ask people who actually wrote Express. Not something we can speak to here.
I'm new to using the URL Rewrite module and I'm having trouble with what I thought would be a simple URL rewrite for forum threads (using IIS 7.5)
I need to rewrite:
/forum/100/2534/friendly-title
or:
/forum/100/2534/334/comment/friendly-thread-title
to:
/forum/?forum=100&thread=2534&post=334&postType=comment
The rule that I have written (not working) is:
^forum/([1-9][0-9][0-9]*)/([1-9]*)/(([1-9]*)/(post|comment)/)?([a-zA-Z0-9-]{5,50})$
Which maps to:
/forum/?forum={R:1}&thread={R:2}&post={R:4}&postType={R:5}
I'm getting a 404 error.
It's correct that {R:4} and {R:5} are empty when you use the first URL. That's because there are no values for these fields. The RegEx still matches though so the URL will still be rewritten. Your code should properly handle empty values for the post and postType querystring parameters to display the entire thread and not just a specific comment (at least that what I assume is suppose to happen).
By the way, a more logical URL structure would be:
/forum/100/2534/friendly-thread-title/comment/334
This won't help you this this particular problem though but just on a side note.
I don't know how to rewrite URLs of this type:
mywebsite/param1-val1-param2-val2-param3-val3-param4-val4.html
that's really simple to do BUT my problem is that my parameters are variables like:
mywebsite/param1-val1-param3-val3-param4-val4.html
or
mywebsite/param3-val3-param4-val4.html
so, the number of parameters is not always the same. It can sometimes be just one, sometimes it can be 10 or more. It redirects to a search script which will grab the parameters through GET querystring.
What I want to do is to not write (on htaccess) a line for every link. The links are pretty simple in that form separated by a -(hyphen) sign.
Rather than rely on complex rewrite rules, I would suggest a simple rewrite rule and then modifying the code of your web application to do the hard part. Supporting this kind of variable parameters is not something that a rewrite rule is going to be very good at on its own.
I would use the following rewrite rule that intercepts any url that contains a hyphen separator and ends in .html
RewriteRule ^(.+[\-].+)\.html$ /query.html?params=$1
Then in your web application can get the parameters from the CGI parameter called params . They look like this now param1-val1-param3-val3-param4-val4. Your code should then split on the hyphens, and then put the parameters into a map. Most web frameworks support some way of adding to or overriding the request parameters. If so, you can do this without doing invasive modifications to the rest of your code.
I have two models called Lessons and Parts. Every lesson has many parts. I'm new to MVC and to Codeigniter but the desired URL is this:
example.com/lesson/2/part/3
I assume I'm supposed to do this with rewrite rules in either the .htaccess or the routes. I need help to get this right and an explanation of the rewrite rule or the route would be perfect.
Maybe Part is actually a method of the lesson's controller?
The CodeIgniter User Guide has a useful page on URI Routing.
You can do something like this to change it so that part is a method in the lesson controller:
$route['lesson/(:num)/part/(:num)'] = "lesson/part/$1/$2";
The guide is somewhat inconsistent in its inclusion of the brackets, so if that doesn't work then try this instead:
$route['lesson/:num/part/:num'] = "lesson/part/$1/$2";