nginx and htaccess rewrite rule with optional parameters - php

I'm trying to make a rewrite rule in NGINX and .htaccess. Now, I have a link http://project2.local/recordings which can be accessed like that, but it has an optional parameter camera. So you can access the link also like this: http://project2.local/recordings/camera/1, now it also has another option. If you go to http://project2.local/recordings/20180118-110222 (where 20180118-110222 is a querystring), you need to "link" to another .php file, so I can run php scripts in seperated files. But I have no clue on how I should do that... I currently have this for nginx:
location /camera {
rewrite ^/camera/([^/]*)$ /camera.php?camera_id=$1 last;
}
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /camera\.php\?camera_id=([^\&\s]+)
RewriteRule ^/?camera\.php$ /camera/%1? [L,R=301]
RewriteRule ^camera/([^/]*)$ /camera.php?camera_id=$1 [L]
Now that is for another page, but how can I do this for the page recordings?
It needs to have or no parameter, or one parameter (and stay on the current file) or /camera/parameter, which links to another .php file

You can use these rules with different patterns:
RewriteRule ^recordings/?$ recordings.php [L,NC]
RewriteRule ^recordings/camera/(\d+)?$ recordings.php?camera_id=$1 [L,NC,QSA]
RewriteRule ^recordings/(\d+-\d+))?$ another.php?param=$1 [L,NC,QSA]

Related

removing .php extension and creating dynamic url from htaccess

I have following code for hiding php extension and redirecting user to a non .php url if user adds a .php extension in url for example domain.com/about.php should go to domain.com/about
RewriteBase /
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
But the problem is that I want to create dynamic url where domain.com/abc?slug=fased should be domain.com/abc/fased but its not working what to do I have used the code RewriteRule ^abc/([^.]+)$ abc?slug=$1 and RewriteRule is on
Try it like this instead in the root .htaccess file:
Options -MultiViews
RewriteEngine On
# Redirect to remove ".php" and optional "slug" query string
RewriteCond /%{QUERY_STRING} ^(?:(/)(?:slug=([^/&]+))|/.*)$
RewriteRule ^([^./]+)\.php$ /$1%1%2 [QSD,R=301,END]
# Redirect to remove "slug" query string (when ".php" is not present on URL-path)
RewriteCond %{QUERY_STRING} ^slug=([^/&]+)$
RewriteRule ^([^./]+)$ /$1/%1 [QSD,R=301,END]
# Rewrite to append ".php" extension
# Handles both "/about" (empty "slug") and "/abc/fased"
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^./]+)(?:/([^/]+))?$ $1.php?slug=$2 [END]
In the directives you posted the RewriteBase directive was superfluous.
This handles both scenarios. ie. Requests for /about and /abc/fased. Requests for /about are internally rewritten with an empty slug URL parameter. However, this also means that /about/foo would also be a valid request (and serve /about.php). But that is really an issue with your "generalised" URL structure not these directives.
The first two rules handle the canonical redirects for SEO (you should already be linking to the canonical URLs internally). For example:
/about.php redirects to /about
/abc?slug=fased redirects to /abc/fased
/abc.php?slug=fased redirects to /abc/fased
However, if any other URL parameters are present on the request then the condition won't match and there will be no redirect.
Clear your browser cache before testing and test first with a 302 (temporary) redirect to avoid potential caching issues.
There's a really good set of introductions to rewrite rules here: Reference: mod_rewrite, URL rewriting and "pretty links" explained
One thing that's really useful to remember is that rewrites don't "make URLs pretty". The pretty URL is the input, and the ugly URL underneath is the output.
Redirects are the other way around, but they just tell the user to go somewhere else; the somewhere else has to work first.
With that in mind, your requirements are this:
a request for abc/fased should serve abc.php?slug=fased; and so on for any "slug"
a request for def should serve def.php, and so on for anything matches a PHP file and isn't already covered by rule 1
a request directly for def.php should tell the user to go to def instead
So:
# Rule 1
RewriteRule abc/(.*) abc.php?slug=$1 [END]
# Rule 2
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [END]
# Rule 3
RewriteRule ^(.+)\.php$ /$1 [R,END]
Only one rule can match each request, because they all have the END flag, and they'll be processed in order. You could put Rule 3 first, but it will only make a difference if you have a file called "abc/something.php" or "something.php.php".

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

mod_rewrite with name replacing ID

I have a page on my website that dynamically displays content. The URL structure is mywebsite.com/giveaway/giveaway.php?id=(any number)
I wish to change that dynamic URL into a static/friendly URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id.
In my .htaccess file found in my root folder i have the following:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
The current .htaccess file removes .php and also redirects and changes a URL on my site from
mywebsite.com?page=number to mywebsite.com/page/number
I know you have to get the name corresponding to the ID in php from my database and use that, but im not sure how to write the code in the .htaccess file or how to use and pass the results from the database. Thanks for any help!
I don't know if that is what you mean, but you can't connect the rewrite engine, which is part of the underlying Apache server layer, to the database, which has to be accessed by php.
So you can't rewrite the "name-of-giveaway-corresponding-to-id" to the id directly, you need to rewrite it to something like giveaway.php?nameofgiveaway=(name of giveaway) and then search the database for that string.
Your way to go is to add a rewrite rule like
RewriteRule ^giveaway/([^/]+)$ giveaway.php?nameofgiveaway=$1 [L,QSA]
(not tested, so forgive me if something is wrong) and search for $_GET['nameofgiveaway'].

Url Rewriting not working with cakephp

I have an application , stored in a sub directory of my domain v2. I have set up my .htaccess file as follows (in the directory mydomain.com)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ v2/$1 [L]
In my knowledge my urls should be rewritten so they don't contain the /v2 in them.
Which does not happen , when I check the value of $this->base and $this->webroot it is www.mydomain.com/v2
when it should be just www.mydomain.com, Is there any way I can change this value.
So What I want to achieve is to rewrite urls so www.mydomain.com/v2/products appears in address bar as http://mydomain.com/products
The only thing those rules do is take a request for www.mydomain.com/products and internally serve the resource at /v2/products. It doesn't do anything about "changing" the URL on the browser. To do that you must redirect:
RewriteCond %{THE_REQUEST} \ /v2/([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301]
And include that along with the rules that you have.

Rewrite url which redirects to page on webserver

I am trying to get a page with a query string to redirect to a nicer looking url then get that url and transfer it back to the original query string but without redirecting (i.e. without changing the url)
At the moment I am getting a redirect loop (for obvious reasons) but I was hoping for a way to stop this.
This is my code in my htaccess file
#rewrite search querystring
#/search/'apartment'/2_bedrooms/price_0-500000/town_W4/development_18,SW5/
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) /search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
/search/$1/$2_bedrooms/price_$3-$4/town_$5/development_$6 [R,L]
RewriteRule ^(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6? [R,L]
So what it is meant to do is:
user has been taken to:
http://www.domain.com/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
This page is the actual page on the server where the data is coming from, however I want the user to see.
http://www.domain.com/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
Is it possible to do this without a redirect loop.
Thanks for your help
EDIT I'm thinking it could be done with the rewrite flags but I'm not sure, I'm quite new to the Rewrite Engine
Edited:
Here is a complete (and working) solution for you:
RewriteEngine On
# User gets here:
# http://localhost/search/?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
# He is explicit redirected to here:
# http://localhost/search/dev/2_bedrooms/price_0-10000000/town_W1/development_W1/
# Internally, apache calls this:
# http://localhost/search.php?propertytype=dev&bedrooms=2&minprice=0&maxprice=10000000&location=W1&development=W1
RewriteRule ^search/([^/]+)/([^/]+)_bedrooms/price_([^/]+)-([^/]+)/town_([^/]+)/development_([^/]+) search.php?propertytype=$1&bedrooms=$2&minprice=$3&maxprice=$4&location=$5&development=$6 [NC,PT]
RewriteCond %{QUERY_STRING} propertytype=([^/]+)&bedrooms=([^/]+)&minprice=([^/]+)&maxprice=([^/]+)&location=([^/]+)&development=([^/]+)
RewriteRule ^search/(.*)$ /search/%1/%2_bedrooms/price_%3-%4/town_%5/development_%6/? [R,L]
It assumes you put .htaccess in server root and that there is a file search.php in root too.
Original:
I think you can use PT and QSA Rewrite Rule flags (http://httpd.apache.org/docs/current/rewrite/flags.html) in your first rule
Use PT for server-side redirection (it will not change the URL for the user/browser, but will for your server-side scripts)
Use QSA if you wanna carry the query while doing this redirection
You can redirect all requests that don't target an existing file to a specific php-script, for example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT,QSA]

Categories