I have this bit of .htaccess:
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %{DOCUMENT_ROOT}/u_pages/%1.php -f
RewriteRule .* u_pages/%1.php? [L]
Using an online converter I get this:
location / {
if ($request_filename ~ index\.php){
rewrite ^(.*)$ /u_pages/%1.php? break;
}
}
-Doesn't work.
What I'm trying to do:
I have a bunch of plain .php documents in /u_pages -- e.g. 123.php
I need these to have URLs like this:
example.com/index.php?id=123
The document NAME corresponds with the number after id=
Any help is greatly appreciated, thank you.
Related
I've searched for other solutions to rewriting a URL using the .htaccess file at the root of my server, but the language is pretty confusing to me, and I can't seem to get it to work. How would I go about changing:
http://domain.com/share.php?media=059ogbuq70
to:
http://domain.com/059ogbuq70
I found code similar to this, and tried it, but it didn't seem to work:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^media=([0-9]+)$
RewriteRule ^$ share.php?media=$1 [QSA,L]
My PHP:
<?php
$media_id = $_GET['media'];
$url = file_get_contents("https://api.wistia.com/v1/medias/" . $media_id . ".json?api_password=my_key");
$json = json_decode($url, true);
$name = $json[name];
$origimg = $json['thumbnail'][url];
list($image, $size) = explode('?', $origimg);
$video = $json['assets'][5][url];
?>
I then echo the variables where I need them on my page.
Thank you!
To internally rewrite "pretty" URLs so that query strings are passed to PHP, I recommend the following .htaccess file:
RewriteEngine on
RewriteRule ^([^/]+)/?$ share.php?media=$1 [L]
The regex matches:
One or more characters that are not a backslash, optionally followed by a backslash.
Everything but the optional trailing slash are captured in $1 and rewritten as the "media" query string variable.
For example:
http://domain.com/059ogbuq70/
rewrites internally to
http://domain.com/share.php?media=059ogbuq70
Which means that:
$_GET['media'] = 059ogbuq70
You might find this mod_rewrite cheat sheet helpful for building your regex.
Also, you can test your rewrites here.
You need two rules for what you are trying to do. You can place this in the file called .htaccess
RewriteEngine on
#redirect all old URLs to the new rewritten URL
RewriteCond %{THE_REQUEST} ^GET\ /+share\.php\?media=([^&\ ]+)
RewriteRule ^ /%1? [R=302,L]
#rewrite folder path internally to share.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ share.php?media=$1 [QSA,L]
I'm trying to get pages that call a variable (from my OLD site) to point to the root level of my blog... using my htaccess file. My knowledge of the htaccess syntax, isn't very good...
For example, any of these pages...
www.example.com/blog/?paged=100
www.example.com/blog/?paged=84
www.example.com/blog/?paged=3
... will ALL be rewritten as ...
www.example.com/blog/
I tried...
RewriteEngine on
RewriteRule ^blog/paged=?$ $1/blog/$2 [R=301,L]
... but that only works for paged=1. Any other variable didn't work (i.e. 2, 100). Can someone help with the correct way to do this?
To be able to match query string you need to use RewriteCond like this.
If /blog/.htaccess doesn't exist:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^blog/?$ /blog/? [R=302,NC,L]
If /blog/.htaccess exists:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^/?$ /blog/? [R=302,NC,L]
I'm a beginner using htaccess and I don't know how to do what I want. I'd like to understand what I'm doing so I'd really appreciate if you can help me give me some advices for ver (very!) beginners... :)
I'd like to:
Redirect xxx.php, xxx.html or any extension to xxx (without extension)
Now, my htaccess is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
and it works but just if I write xxx. But if I write xxx.php (I see the page :(, I'd like to redirect to xxx) and if I write xxx.html it doesn't show nothing.
Finally. I've like to rewrite variables to friendly links
i.e. If I have xxx.php?id=1 > I would like to redirect to xxx/username
Thank you in advance
Best wishes and merry christmas! :)
Try this:
I use this code already.
Rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
Put this in your htaccess file && then check for files without extension.
Thanks
Try adding these rules to your htaccess file:
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php
RewriteRule ^ /%1 [L,R=301]
If you need to do the same with with .html extensions, then change the php part of that condition to (php|html?).
Setup
I have my file structure set up as so:
/root
|
/api
|
/Slim PHP framework
index.php
|
index.php
The index.php inside the Slim directory contains the routes required to retrieve the JSON data from a Mongo database. E.g.
$app->get('/users(/:id)', function($id = null) use ($app, $collection) {
/* Do mongo search and echo json_encoded data back */
});
I have an .htaccess file contains: which removes the .php extension from files under the root.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php`
Question
I can access my JSON data using url: http://localhost:8888/root/api/index.php/users.
However, what I would like to do is access the data using the url: http://localhost:8888/root/api/users
If I understand correctly, what about this?
RewriteRule ^api/users/(.*)$ /root/api/index.php/users/$1 [L]
RewriteRule ^api/users$ /root/api/index.php/users [L]
That should allow this URL.
http://localhost:8888/api/users/1
and it will allow this too
http://localhost:8888/api/users
EDIT:
That should allow you to add a number after users. Also shortened the URL so you don't have to include root in the URL as well.
Assuming, your .htacces is located in website root "/"
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_URI} !/index.php [NC]
RewriteRule ^(.*?/api)/(.*)$ $1/index.php/$2 [NC,L]
This would redirect
http://localhost/root/api/users/1 => http://localhost/root/api/index.php/users/1
http://localhost/root/api/data/20 => http://localhost/root/api/index.php/data/20
One thing of note, the sequence of rules is also important, so if you need to rewrite/redirect http://example.com/users AND http://example.com/users/ to http://example.com/userinfo/ while at the same time rewriting http://example.com/users/uname to http://example.com/scripts/script.php?id=uname then this one should work: RewriteEngine on
RewriteEngine on
RewriteRule ^users/$ /userinfo/ [L]
RewriteRule ^users/(.*)$ /scripts/script.php?id=$1 [L]
RewriteRule ^users$ /userinfo/ [L]
There could be a better solution with one line less but this one works for me.
so my url is currently:
http://www.mywebsite.com/search.php?keyword=stack+overflow
I'm wanting to have it accessible as:
http://www.mywebsite.com/?s=stack+overflow
Now I've also implemented rewrites for .php files as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I'm a little confused now because the search.php is being rewritten to just search but is still accessable with search.php, so when I create my rewrite rule do I rewrite search.php or just search?.. either way I've tried and failed to accomplish it lol.
What should I add to have my desired url? Help is much appreciated :)
It's impossible to change parameter name with mod_rewrite. The easiest way to achieve what you want is to add these lines to .htaccess:
RewriteCond %{QUERY_STRING} ^s=
RewriteRule .? search.php [L]
and modify search.php to react to s get parameter the same as it reacts to keyword.
An alternative is to keep .htaccess intact and add these at the top of index.php:
if (!empty($_GET['s'])) {
$_GET['keyword'] = $_GET['s'];
require __DIR__ . '/search.php';
exit;
}