URL Rewrite rewrite x.php?id=1 - php

I've searched the site and got far enough where I've been successful at rewriting to a clean URL. Just need a bit more help.
I have a page with a record that I have successfully rewritten to a clean URL like so:
domain/record.php?id=1685 > to > domain/record-Gavin-Rees-1685 using the below:
.htaccess file:
RewriteRule ^record-(.*)-(.*)$ /record.php?id=$2 [L]
This in my php file:
$temp=str_replace(' ','-', $record [record_name]);
$temp=str_replace('.','', $temp);
<a href='/record-". $temp ."-".$record[id]." '>
This works perfect. The problem is.
I cannot get it to rewrite the other way so if you go directly to:
/record.php?id=1685 it still exists. i tried > RewriteRule ^record.php?id=$ /record-(.*)-(.*)$ [R,L]

This isn't possible in the .htacces rewrite rules, because you couldn't define a general rule, which knows about your software internals. If you want to do this in your .htaccess file, you have to create a rule for every single id which would result in a very large and unhandy .htaccess file.
The better way to get an redirection for direct script calls is, to do the redirect in the php file itself and set the http status codes(i.e. 301 - permanently moved).
The get the requested url, you could use $_SERVER['REQUEST_URI'] and check for /record.php at the beginning

Related

Is it possible to get the original URL after a 302 .htaccess RedirectMatch?

Hello guys. I started coding my own "URL shortener". The basic idea is you use example.com/12345 to redirect to another URL. This "matching" is done by using .htaccess to redirect stuff towards a script that does (irrelevant for us now) stuff.
My .htaccess currently looks like this:
RedirectMatch 302 ^/\w{5}$ /redir.php
The redirect matches any string of exactly 5 and sends it toward my PHP script where the actual redirection to the expanded URL take place. The only problem is that I was unable to find a proper way of getting the original URL, the matched one into a variable.
As a sidenote the whole thing happens on a VPS set up by me with minimal knowledge, so if this problem can originate from a missing config ($_SERVER['HTTP_REFERER'] doesn't work), then expect my configs to not be 100% correct and by standards.
EDIT: changed from RedirectMatch to RewriteRule, still doesn't work.
RewriteRule ^\w{5}$ /redir.php [R,L]
you can use the following rule:
RewriteRule ^(\w{5})$ /redir.php?redir=$1 [R,L]
this will send the 5 letter string as querystring param redir. Which you can access in redir.php as:
$_GET['redir']
Edit: Or as #LawrenceCherone have suggested you can use $_SERVER['REQUEST_URI'] in redir.php. But for that you have to use NC flag in .htaccess instead, Like:
RewriteRule ^(\w{5})$ /redir.php [NC,L]

Fetching the URL parameters with PHP after IIS URL rewrite

I have a PHP website,
e.g.
http://www.test.com/rewrite-test/s/z2SZhBL
This was previously on Apache which had a rewrite rule to trap the "z2SZhBL" using the $_GET['id'].
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?id=$1 [L]
I need to move this website onto IIS and I need to get the URL rewrite rule working. I have tried these examples.
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
So in the folder "rewrite-test" I have an index.php which is echoing out the $_GET variables and there are no get variables coming back they are all empty.
I can get the variables using this workaround.
$params = explode( "/", $_SERVER['HTTP_X_ORIGINAL_URL'] );
print_r($params);
Is this best solution?
IIS Manager has a good Import Rules tool. See full walk through here: http://www.iis.net/learn/extensions/url-rewrite-module/importing-apache-modrewrite-rules
Open URL Rewrite
In Actions Pane, click Import Rules
Browse to your .htaccess file (Or Copy/Paste them in)
Review the Converted Rules
Rename the rule as desired
Click Apply Link to save the converted rules

Using .htaccess to use subdirectory as a parameter

I've had a real tough time trying to search for the exact htaccess code that will allow me to do the following:
Visiting: http://www.domain.com/wildcard
Should show: http://www.domain.com/
But the URL should still read: http://www.domain.com/wildcard
So basically a transparent redirection... seems fairly straight-forward, but surprisingly hard to search for. The PHP in index.php is already set up to parse the subdirectory and read it as a parameter, but unfortunately my client never supplied me with the .htaccess file. #developerproblems
Thanks!
You just need this ErrorDocument 404 line at top of your .htaccess:
ErrorDocument 404 /
This will show home page for any request that is not a file or directory and results in a 404.
We will suppose that you have to receive a url parameter called param so your rewrite rule should be:
RewriteEngine On
RewriteRule ^([^/]*)$ /?param=$1 [L]
By this way any http://www.domain.com/AnythingHere will render the contents of http://www.domain.com/?param=AnythingHere so the home page is rendered.
However, such solutions, they are not change the contents, they may leads to SEO problems for repeated contents, So the solution of anubhava is better for SEO according to your usage.

htaccess redirect not working

so I have this htaccess entry:
RedirectMatch /([a-zA-Z0-9]+).php /dirA/$1.php
The goal is that any .php that is on the root directory should be redirected to /dirA/*.php
eg. suppose I make the request
domain.com/something.php
it should instead redirect to
domain.com/dirA/something.php
However when I put that entry in my .htaccess file and then I go to domain.com/something.php
it instead returns
"The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for
this address in a way that will never complete."
Any idea how I can modify my htaccess to accomplish what I want to do?
Updated Question
Also is there a way to make it so that it only redirects if the file doesn't exist in the root directory...hence if x.php exists in root, serve that x.php otherwise redirect to dirA/x.php
mod_rewrite is an overkill for this, you were on the right track with RedirectMatch. Your rule, however, is a bit faulty: the regex /([a-zA-Z0-9]+).php matches all string that contain the specified substring, so it matches "/foo/bar/baz.php", but also "dirA/foo/bar.php" (and even "/foo/bar.php/baz.php"I. Your redirection ended up in an endless loop because there was no stop condition: /dirA/foo.php was redirected to /dirA/foo.php.
You can remedy the situation by using anchors in the regex:
RedirectMatch ^/([a-zA-Z0-9]+).php$ /dirA/$1.php
As for your second question: that might indeed call for mod_rewrite. Something along these lines should do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+).php$ /dirA/$1.php [R=301]
I haven't tested it, but this should get you started. Make sure to check out the manual for details, or just search around on SO, there are tons of questions about this.
Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+).php /dirA/$1.php [R=301,L]
This one should be just fine
RewriteRule ^(.*)$ /subdir/$1

How to add a custom word to my domain URL for every request?

Consider my domain name is
www.mydomain.com
Consider a page request
www.mydomain.com/user/register
I want to add a custom word after base URL for every request inside mydomain.com.example
www.mydomain.com/customword/
www.mydomain.com/customword/user/register
Can we do this using URL rewriting in htaccess file ?
Actually it should execute 'www.mydomain.com/user/register' internally...but externally the URL should look like www.mydomain.com/customword/user/register.
You could create the directory "register", and put an index file inside it that performs the action.
That's probably the simplest way without url rewriting anyway.
UPDATE (since the question was updated)
In .htaccess:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-.]+)/user/register/?$ user/register.php?customword=$1
register.php will receive a GET request:
//User went to www.mydomain/word/user/register
echo $_GET['customword']; // will return word in this case
Make sure that you have mod_rewrite enabled :)
Yes, you can do it with htaccess
Here is an example which will add a trailing slash with url if it doesnt contain trailing slash
http://enarion.net/web/htaccess/trailing-slash/
edit formatting updated
If you are serving one site from this then the following should work:
Edit your .htaccess file to do a url rewrite
accessing www.yourdomain.com/user/registry will actually server content from www.yourdomain.com/customword/user/registry
RewriteEngine On<br>
RewriteCond %{REQUEST_URI} !^/customword/<br>
RewriteRule ^(.*)$ /customword/$1
You haven't mentioned what kind of site you;re using..eg: PHP, MVC etc as you could do similar thing in there as well.

Categories