PHP Rewrite Rules - .htaccess

The actually URL which my app uses is:
http://site.com/search.php?search=iPhone
but I would like it to be possible to achieve the same with
http://site.com/iPhone
I have no experience of rewrite rules, how can I set this up?
The solution has worked but the new URL is displayed in the address bar. I thought it would have been possible to set this up so that it appears as though the page location is
http://site.com/iPhone
without changing to display
http://site.com/search.php?search=iPhone
Is this possible? Thanks.

Create a file called .htaccess in the root of your website and put this in it.
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*) search.php?search=$1 [R]
Should do the trick.
I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg instead of mysite.com/IPhone use mysite.com/search/IPhone which would work like
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^search/(.*) search.php?search=$1 [R]
This makes it easier to have normal pages that arnt redirected, such as about us or a basic homepage.
As Chris says, this is not PHP but Apache that does this, and whether it works can depend on your hosting setup.

You need to specify something like this in your .htaccess file:
RewriteEngine on
RewriteRule /(.*) /search.php?search=$1
Check also:
mod_rewrite: A Beginner's Guide to URL Rewriting
Module mod_rewrite, URL Rewriting Engine

Rewrite rules aren't part of PHP as far as I'm aware, but Apache (specifically mod_rewrite) or whatever server you're using. For Apache, you need on the server to have a file called .htaccess, and in it put something like:
RewriteEngine on
RewriteRule ^(\w+)/?$ /index.php?search=$1
^(\w+)/?$ is a regular expression - it matches any word of 1 or more characters, followed by a / maybe. So it changes site.com/iPhone into site.com/index.php?search=iPhone. Sound about right?

Related

htaccess rewrite URL from subdirectory

I've been trying to set up a rewrite rule in our htaccess. We have a search component that directs to the URL:
http://www.domain.co.uk/component/search?searchword=word&searchphrase=all&start=10
We'd like to show it as:
http://www.domain.co.uk/search/word/10
I've been trying this with no avail so far, this is what I have.
RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)$ /component/search/?searchword=$1&searchphrase=all&start=$2 [L]
Is there something missing? Could other rules be interfering with it?
At least you should escape the slashes and the question mark
RewriteRule ^search/([^/]*)/([^/]*)$ /component\/search\/\?searchword=$1\&searchphrase=all\&start=$2$ [L]
And you should not put ampersands (&) in your query string - use %26 or & instead...
You generate your link in the first place as
http://www.domain.co.uk/search/word/10
and use .htaccess such as
RewriteEngine On
RewriteRule ^search/([^/]+)/([^/]+)/? /component/search/search.php?searchword=$1&searchphrase=all&start=$2 [L]
to rewrite it to the dynamic form your search.php script can handle. Your attempt basically looks OK (except that you need search.php, not search), so you might want to check if "mod rewrite" is enabled on your server. You might have to wrap the above code in
<IfModule "mod_rewrite.c">
RewriteEngine...
</IfModule>
Consult with your hosting service.

.htaccess Rewrite rule doesn't work

I know there are already a ton of these questions but I don't get it...
I'm trying to rewrite my /register.php to /register but I can't.
.htaccess code
RewriteEngine on
RewriteBase /
RewriteRule ^register$ register.php [L]
Does any of you know the answer?
Sorry for the stupid question but I'm kinda new to the whole .htaccess stuff...
Do you have enabled the Apache module mod_rewrite?
I use this pattern to create user-friendly URLs. It's pretty variable. It means, that every address in format letters-numbers with a certain file extension (PHP or HTML) will be translate into letters-numbers.php (for example).
RewriteRule ^([a-z]+)-([0-9]+)\.(php|html)$ /$1-$2.$3 [L]

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

.htaccess - add language sub directory by default "sv/" (redirect)

Trying to orientate myself through the .htaccess jungle, I've now narrated down my rewrite problems to one - adding a language suffix upon entering the page / typing in an address that doesn't have such one.
For example: a visitor types mysite.com/kontakt. This should take them to the default language of the site, i.e. mysite.com/sv/kontakt. Or, just typing in mysite.com should take him/her to mysite.com/sv/.
I'm developing this site locally using MAMP, and the site is located in a subdirectory, and here's the tricky part... How do I sort this out in the .htaccess file?
Current code used:
RewriteBase /mysite/
RewriteCond %{REQUEST_URI} !^(sv|en)
RewriteRule ^(.*)$ sv/$1 [L,R=301]
And that takes me from localhost/mysite/ to localhost/sv/. Not exactly right.
Try this one:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^(?!(?:sv|en)/)(.*)$ sv/$1 [R=301,L]
This needs to be placed into .htaccess file into /mysite/ folder.
If accessing localhost/mysite/sv without trailing slash, the rule will redirect it to localhost/mysite/sv/sv as it expects that the language part (sv or en) will be followed by the slash /.
P.S.
And look into setting up a VirtualHost -- then there will be much less (if none at all) of such "tricky parts" with accessing your website via http://localhost/mysite/sv/kontakt (I'm sure you will agree, that http://mysite.dev/sv/kontakt sounds and looks better).

PHP and .htaccess redirect woodoo

I'm a little stuck with with my .htaccess redirect.
It was working find while I was with PHP4 but the recent move to a new host with PHP5 have changed things for which I've no clue.
I'm working on a URL shortening service. Here, for a URL like http://example.com/e72b0f, it gives me http://example.com/forward.php?e72b0f
Earlier with my .htaccess file, the "forward.php?" was masked (hidden). How can I bring back this behavior. Here is the .htaccess for your reference.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \/([0-9a-z]{6})$ [NC]
RewriteRule ^(.*) http://example.com/forward.php?%1 [L]
Btw, I also do not rule out the issue being in the PHP Script. The developer that did it for me is too busy to look at it.
If you rewrite to an http:// URL that the server doesn't think is in the site, mod_rewrite will do a redirect instead of just a rewrite. In order to see if this is happening, make a page that has nothing in it but
<?php echo $_SERVER['SERVER_NAME']; ?>
and see if says it's going to "nsfw.in".
Either way, you should be able to strip off the http:// nsfw.in from the beginning of the URL and just rewrite it to /forward.php?%1. You may need to add a PT flag in order for it to be interpreted as a URL and not a FS path.

Resources