I am a pretty skilled programmer, but when it comes to RegEx and rewriting, I am a total n00b. I want to convert a URL from
http://www.example.com/lookup.php?id=1
to
http://www.example.com/lookup/1/item/
where "item" refers to the name of an item in a database that is being looked-up.
I'm using LAMP (Linux, Apache, MySQL, PHP) and I cannot, for the life of me, figure out how to convert the URLs so they are SEO friendly.
Simple .htaccess example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lookup/([a-z0-9\-]+)/item/?$ /lookup.php?id=$1
</IfModule>
This will match any alphanumeric (also will recognise dashes) string of any length as the 'id'. You can limit this to just numeric by changing the regex to ([0-9]+).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lookup/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /lookup.php?id=$1&view=$2
</IfModule>
This one will match /lookup/123/some-text/ to /lookup.php?id=123&view=some-text
Take a look on htaccess rewrite urls! :)
Here's your example:
RewriteRule ^lookup/(\d+)/(.*)$ /lookup.php?id=$1&name=$2
When you access lookup/123/my-product/, it'll call the lookup.php?id=123&name=my-product file internally.
Do you want to redirect from the old URL to the new? Or are you looking to read in those "friendly" URLs and then have them behave like the old URL?
If it's the latter, try taking a look at Net_URL_Mapper as a way to parse and redirect those links fairly easily.
Related
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.
Yes, I searched the site and found many examples, but my .htacces skills are very limited, so NONE of examples worked for me after trying to modify them to suit my needs.
Put simply, if URL is like "index.php?/............" user should be redirect to example.com (ideally, to example.com instead of example.com/index.php) immediately.
I need this because I have an old domain that was used for forum, so I get thousands of requests like "example.com/index.php?/whatever" and all these virtual pages display the same content, which hurts my rankings badly.
My index.php file has NO parameters at all (but some other files have, so the rule shouldn't affect other files).
Thanks a lot!
Try adding these rules to the htaccess file in your document root (preferably before any other rules you have there)
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/
RewriteRule ^index\.php$ /? [L,R=301]
How to rewrite this url "www.domain.com/index.php?route=custom/static/page" to "www.domain.com/page" in htaccess file, basically just want to take out index.php?route=custom/static/ from urls.
I don't know regex so I tried http://www.generateit.net/mod-rewrite/, but it only generates
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
which doesnt remove 'custom/static' from URLs, I tried a few other examples as well but only removes index.php? and doesnt pass variable, any help is appreciated.
Do you know the concept of using mod-rewrite?
In your question you have mentioned to use mod-rewrite to redirect
"www.domain.com/index.php?route=custom/static/page",
Here $_Get['route']="custom/static/page"] $url_parameter=$_Get['route']
to
"www.domain.com/page" [here $_Get['route']="page"],
So now you can mannually add "custom/static/" to the obtained value of $_Get['route']. as $url_parameter="custom/static"+$_Get['route'] //For PHP
Using your mod_rewrite you can fulfill your demands,
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
But if you need out of box solution using .htaccess then I suggest learning "rewrite-engine" instead of using generating tool
Something similar had been discussed here, but this is slight different.
How to tell .htaccess to rewrite any requested page (regardless of it's depth from the site root) with site's index page, but with requested URL as a $_GET parameter, so it can be further handled by php, depending on it's contents, or depending on URL itself.
I was trying something like
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*) index.php?page=$1
but obviously I do something wrong.
Thanks in advance.
Try working on your RegEx. Maybe something like this:
RewriteRule ^([a-zA-Z0-9\-]+)([\/]*)$ index.php?page=$1 [NC,QSA,L]
Basically you can generate the rewrite code using an online tool like http://www.webtoolhub.com/tn561403-htaccess-url-rewrite.aspx by entering the desired parameters. Just put the url structure and it will write the .htaccess code that you can use or modify according to your needs.
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?