I am fairly new to Nginx and doing good so far. I am trying to cut down on the rewrites so I am writing new ones as I will not need to edit them anymore.
I have the following URL:
http://example.com/en/this-is-my-life
Which needs to be rewritten like"
http://example.com/index.php?page=this-is-my-life&lang=en
Now because "page=" and "lang=" are increasing I need to have a rewrite that will take these 2 parameters of the URI and rewrite them.
I have tried the following but it is not working. I am using nginx-1.4.2.
rewrite ^/([a-z])/([a-z-])/$ /index.php?page=$2&lang=$1 last;
Any ideas why it is not working?
Related
I'm trying to rewrite the following long URL in htaccess but keeps redirecting to error page
Existing URL:
https://www.exampledomain.com/stuff-for-sale?location_value=California%2C+USA&property_status=&adm_lvl_1_sn=CA&country_sn=US&location_type=administrative_area_level_1&stateSearch=CA&swlat=32.528832&nelat=42.0095169&swlng=-124.48200300000002&nelng=-114.13121100000001&lat=36.778261&lng=-119.41793239999998&faddress=California%2C+USA&place_id=ChIJPV4oX_65j4ARVW8IJ6IJUYs
I'd like to rewrite simply as:
https://www.exampledomain.com/california-stuff-for-sale
Is this possible? I know that with this approach I would have to do 50 rewrites for each state but the system requires all the parameters to create a proper search.
This is what I tried so far with no luck:
RewriteRule ^/stuff-for-sale?q=&location_value=California%2C+USA&adm_lvl_1_sn=CA&country_sn=US&location_type=administrative_area_level_1&stateSearch=CA&swlat=32.528832&nelat=42.0095169&swlng=-124.48200300000002&nelng=-114.13121100000001&lat=36.778261&lng=-119.41793239999998&faddress=California%2C+USA&place_id=ChIJPV4oX_65j4ARVW8IJ6IJUYs /california-stuff-for-sale[L]
i have an issue regarding mod_rewrite ... the url rewrite sems to work fine ... only thing is that the url in the address bar remains the same as it was before rewrite
for example: i want to rewrite www.site.com/page.php?page=merchandise to www.site.com/merchandise. now when i write the url i go to the right page (www.site.com/page.php?page=merchandise) but the address in the address bar remains ( www.site.com/page.php?page=merchandise) where it should be (www.site.com/merchandise) ... it works fine on the local environmnt...but problems occurs in live environment ...
I'm currently using simple RewriteRule.
The rewrite rule is as followed:
RewriteRule ^merchandise$ http://www.mysite.com/page.php?page=merchandise [NC]
Its pretty simple and should work. But it does not hide the actual address in the address bar. That's my problem.Otherwise it is going to right page.
Any help is appreciated.
You are describing the way mod_rewrite has been made for, and you are surprised that is works this way. This is funny :)
And it seems you're mixing things.
mod_rewrite is here to hide complex URLs and to make them simpler (generally speaking).
You are wrong when you say
"www.site.com/page.php?page=merchandise" to www.site.com/merchandise"
The real thing is probably the opposite:
you want the Net surfer to type "http://www.site.com/merchandise"
you want your server to change "http://www.site.com/merchandise" to "http://www.site.com/page.php?page=merchandise"
what you didn't get is that 99.9999% of the time webmasters don't want the client to see the real page is "http://www.site.com/page.php?page=merchandise"
...
And that's what does mod_rewrite: it looks at incoming URLs, and modify them internally. So the webdevelopper (= you) can transform the URLs using RewriteRule's and make whatever you want but it's always into the server. The only exception is when you want to explicitely redirect the client to another URLs, then you can use the "[F]" directive which means "Forward".
Hope this helps
I have a url eg. www.example.com/user.php?user_id=9 , where the user_id field maps to one of the pk in the user table . I don't want the url to be like this , instead i want to have a url like www.example.com/user/Aditya-Shukla.i am using apache 2 and I understand that mod-rewrite module has sets of rewriting rules which can be used to create url alias.
My question is
I have all href in the form www.example.com/user.php?user_id=9. So to change the url I suppose i have to change all the href's to the www.example.com/user/Aditya-Shukla and for rewriting the rule do a query to get a record?
Is there a better solution .
No, mod_rewrite does not have sets of rewriting rules. It rather provides directives to build rules based on regular expression patterns that can be combined with additional conditions.
In your case you would build a rule that takes any requested URL path that starts with /user/ and has another path segment following and rewrites it internally to your user.php, like:
RewriteEngine on
RewriteRule ^/user/([^/]+)$ /user.php?name=$1
The first directive RewriteEngine on is just to enable mod_rewrite. And the second directive RewriteRule … is the rule as described above: ^/user/([^/]+)$ is the pattern that matches any URL path that starts with /user/ (i.e. ^/user/) and that is followed by one path segment (i.e. ([^/]+)$). That request is then rewritten internally to /user.php while the matched path segment behind the /user/ is used as a parameter value for the name parameter ($1 is a reference to the matched value of the first group denoted with (…)).
So this will rewrite a request of /user/Aditya-Shukla internally to /user.php?name=Aditya-Shukla. You can then use that user name and look it up in your table.
You can either add a RewriteRule that will rewrite user/Aditya-Shukla to user.php?user_name=Aditya-Shukla and handle the rest in your code.
RewriteEngine On
RewriteRule ^user/(.*)$ user.php?user_name=$1
Or using a RewriteMap directive to lookup usernames, which will allow to rewrite user/Aditya-Shukla directly to user.php?user_id=9
I presume that within your own site you will always create the canonical form of the URL, i.e.:
/user/Aditya-Shukla
...and you are just having to deal with outside links that are not in canonical form, i.e. "old links" like:
www.example.com/user.php?user_id=9
mod_rewrite may not be suitable for remapping in this situation. I am presuming you may have very many users, and that number may grow. mod_rewrite does have a RewriteMap directive and yes there are ways to generate your map dynamically, but I don't think that would be a good design (to dynamically create a map of userId-to-userName dynamically every time your rewrite rule matches...)
Instead you should simply write your user.php code to lookup the correct userName, assemble the canonical form of URL you want, and send a redirect back to the client. Something like:
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.example.com/user/Aditya-Shukla" );
You should probably also use a 301 redirect (instead of 302) to indicate this is a "permanent" URL change, which will help search bots index your site correctly if it encounters an "old style" URL out there.
-broc
I have been trying to do this for the last 4 hours and have searched everywhere. it would be great if you could help me with nginx rewrite rules.
I am trying to temporarily redirect hxxp://siteA/[dir] to hxxp://siteB/[dir] except for hxxp://siteA/?page=4 which I want to redirect to hxxp://siteB/?page=343
Here are the rules I have
server {
listen 80;
server_name siteA;
rewrite ^/?page=4$ http://siteB/?page=343 redirect;
rewrite ^/(.*)$ http://siteB/$1 redirect;
}
But it seems to redirect hxxp://siteA/?page=4 to hxxp://siteB/?page=4 thus ignoring the first rule.
I have changed http to hxxp since I am not allowed to post links here.
A common problem faced when writing rewrite rules for web servers is assuming that query parameters are part of the script path.
Try matching the query parameters with a rule like the below before your 'main' rewrite rule:
if ($args ~ page=4){
rewrite ^ http://siteB.com/?page=343;
}
How can push a URI like this in CodeIgniter (1.7.1 currently):
example.com/seg1/seg2/seg3/seg4/
or
example.com/seg1/seg2/
etc. through a single class method in a controller whose name does not appear in the URI? In a regular PHP scenario I would use mod_rewrite something like this:
RewriteRule ^([^/]+)/$ myfile.php?one=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2&three=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ myfile.php?one=$1&two=$2&three=$3&four=$4 [L]
(I sanitize and validate the segments extensively in PHP, returning 404 if invalid)
But in Codeigniter I can't figure out how to do this without hard coding at least the first segment, but then using custom routes it still wants to treat the subsequent segments as method calls.
I'm a newbie to CI, but have so far managed to port over the entire existing site with the exception of this part of it. I don't see how all the parts come together on this problem, so any suggestion is welcome.
Clarification: I read through documentation on URI library, routing etc before starting my project, and they were helpful for various things, but this specific problem is not addressed by any of them. I'm not seeing how all segments of the URI can essentially be funneled through a controller that is not named in the URI and where all segments are arbitrary. The routing examples assume you know the value of the first segment. I already know how to remove "index.php" as well as access segments.
Have you looked up URI routing in the user guide and the wiki? They should tell you almost anything about routing, rewriting and accessing the different URI segments.
http://codeigniter.com/user_guide/general/routing.html
http://codeigniter.com/user_guide/libraries/uri.html
http://codeigniter.com/wiki/mod_rewrite/
[Edit:]
Here's the long description:
There is no way to not "hard-code" the first segment, and you can still "hard-code" the second segment.
What you want to accomplish can be nearly done by editing the routes in system/application/config/routes.php:
$route['(:any)'] = 'your_default_controller/index/$1';
$route['default_controller'] = "your_default_controller";
So, the first segment of your URI will be the method of the controller. You can access all segments of your (initial) URI by
$this->uri->segment(n)
You would then use the method index to call the desired function for each request.
On a side note: Why do you want to use an MVC framework for that, as you do not use much of the benefits of MVC?