Rewrite URi with htaccess to related PHP file - php

I have a URi Like this:
http://my.domain.com/clientarea/verify/
and I want it rewrites to this one:
http://my.domain.com/verify.php
I mean when the first URi called, it shows the result of the verify.php at root path.
Anyone knows how I can do it?!

Add this right after RewriteEngine on:
RewriteRule ^clientarea/verify/$ /verify.php

Related

Error 404 when trying to create a pretty url using .htaccess in php

I want to create a pretty rule in php using .htaccess.
something like localhost/search/name instead of using the normal PHP way like search?q=name. But am stuck in implementing it because all my page are now returning error 404 and I don't know why.
Here is my .htaccess file:
RewriteEngine on
RewriteRule ^localhost/?$ search.php?name=$1 [QSA]`
Here is my search.php file:
<?php
echo $_GET['name'];
?>
So when I visit something like localhost/search/myname it returns error 404
What am I doing wrong?
You're going to want to first also include the /search/ as part of the rewrite rule and then i would also suggest adding in the 'L' flag after QSA so the rule is the last rule processed when called.
You then want to capture the name from the url and pass this through as a get param to the view.
I've also ommitted localhost from the URL as you don't need to target this.
try this:
RewriteRule ^search/(.*)$ search.php?name=$1 [QSA,L]

PHP Seo Friendly URL

I'm having some trouble using rewrite engine to achieve my goal with friendly url.
Basically I have the following structure:
- index.php
- down
- index.php
My links in main index are something like this:
download/index.php?down=FILENAME
I would like it to rewrite something like
/download/FILE-NAME ( while pointing for the index.php inside download folder ).
I would like my links in first index could be used as well /download/FILE-NAME
My actual code:
RewriteEngine On
RewriteRule download/^([a-zA-Z0-9_-]+)$ index.php?down=$1
Can anyone help me achieving this? I'm constantly getting error due to I can't do it right.
After that, how can I get the the variable $1 after transform to seo friendly urls? I will have download/FILE-NAME and I need with PHP get the info from FILE-NAME.
So basically in the main index.php I would have a link like /download/FILE-NAME that will open the down/index.php file with the FILE-NAME ( staying something like.
Try this :
RewriteEngine On
RewriteRule ^download/([a-zA-Z0-9_-]+)$ /ifanydirectory/index.php?down=$1
And you can get variable using $_GET['down'] ifanydirectory is the directory name in which index.php exists relative path from root level, if not applicable simply use /index.php
do some experiments you'll get the correct one.
RewriteCond %{REQUEST_URI} ^download/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?down=$1 [L,QSA]
to catch filename in index.php:
$filename = $_GET["down"];
Maybe I've not explain it well because it doesn't work as it's supposed.
I've edited the main question but try to explain better here.
I have a main index.php with links that would be
download/FILE-1
download/FILE-2
download/FILE-3
That links should open the folder down/index.php?down=FILE-NAME.
Isn't supposed that anyone can access "down" folder directly, so I would like to transform links in my main index.php like /down/index.php?down=FILE-NAME to something like /download/FILE-NAME.

URL Rewrite rewrite x.php?id=1

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

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.

Inserting a hash before a directory name with Apache mod_rewrite or PHP?

I have a URL that is like the following:
http://www.example.com/client/project/subdirectory/value/
I would like like a simple way to be able to change/redirect the URL to the following:
http://www.example.com/client/project/#/subdirectory/value/
Once the redirect is complete, the hash needs to be accessible via JavaScript. I'm okay with a full refresh/redirect, just ideally that I write this once and don't have to change it again.
In other words, when the site goes live, the URLs will be structured differently, so that:
http://www.example.com/subdirectory/value/
Will change to:
http://www.example.com/#/subdirectory/value/
Edit:
I have tried using this:
RewriteRule ^profile/?$ #/profile/ [ NC,L]
Which doesn't seem to do anything
Also tried this:
RewriteRule ^profile/?$ /#/profile/ [NC,L]
Which takes me to the root directory
Also tried this:
RewriteRule ^profile/?$ #/profile/ [R,NC,L]
Which adds the whole root path to the server, followed by /%23/profile/
If you have a URL like the following:
http://localhost/tests/redir/subdirectory/value/
And you want to get it redirected to:
http://localhost/tests/redir/#/subdirectory/value/
Place a .htaccess file into the directory of tests/redir with the following:
RewriteEngine On
RewriteBase /tests/redir
RewriteRule ^(.*/)$ #/$1 [R,L,NE]
And you will get the wanted redirect. The R flag plays together with the RewriteBase directive. Also the NE flag is necessary so that you can put # literally into the redirect URI.
The hash is already used by page anchor tags. You might need to replace it with an entity or pick a better character.

Categories