making existing url to seo friendly without making change to table - php

how to make this url to seo friendly urls
items.php?itemid=1&title=my title goes here
to
products/1/my-title-goes-here
how it can be done using only .htaccess not making any change to table or code currently data is access through id, don't even changing the query.

You have to use the Apache module mod_rewrite and the respective rewrite rules in the .htaccess file.
Read this it helped me a lot when I was first starting with it

Something like (not tested)
RewriteEngine on
RewriteBase /
RewriteRule ^products/([^/]+)/([^/]+)$ http://your web site/items.php?=$1&title=$2 [R=301,L]
which matches the patten that starts with products, with the first sub-patten being at least one character long, and anything that is not '/' . Same is true for the second pattern which must end the URL. You then rewrote that into the actual string your software needs.

Related

GET request without needing the ? at the end

I am developing my login system a bit further to look a lot more professional, and I wanted to know how I could turn get requests into simple links so they look a lot more sleeker?
For example for one of my systems a user can search someones elses profile by going to http://www.example.com/user?user=JimmyJones
Thats all fine and dandy but I don't think it looks very good and many other websites don't have this in their links due to some kind of trick I don't know about, as you can see I have gotten rid of the .php at the end which is done using some very simple htaccess.
But how can I change that link above to:
http://www.example.com/user/JimmyJones
Thank you very much for taking your time to read this and I really hope someone can help me out with my little problem, I assume there is some way to do this in .htaccess?
EDIT:
Here are some websites that do it just about how I would like to do it:
imgur.com/user/example
facebook.com/exampleuser
you make .htaccess file in the root dictionary then start it with
RewriteEngine on
then write your rules, For your example it would be like this
RewriteRule ^/?user/([^/]+)/?$ user?user=$1 [L,QSA]
so a full page would be like this
RewriteEngine on
RewriteRule ^/?user/([^/]+)/?$ user.php?user=$1
just for your example.
In .htaccess assuming the Apache web server with the rewrite module enabled, something like this:
RewriteEngine On
RewriteRule ^user/([a-zA-Z]+)$ user.php?user=$1 [L]
The first line says use the rewrite engine.
The second, says match a url that begins (or rather is relative to the .htaccess containing folder) with the pattern 'user' followed by a slash and matching a pattern of any alphabetic characters until the end of the string (not including additional query parameters).
The L flag basically says job done.
If the .htaccess were in the public root:
//example.com/user/JimmieJones
would map to:
//example.com/user.php?user=JimmieJones
However it will not match:
//example.com/user
//example.com/user/
//example.com/user/JimmieJones/
//example.com/user/Freddy_K9
Note that any existing links in your application:
Visit Jimmie's Profile
Would likely need to be updated. And with the example pattern above, the old style urls (previously indexed/bookmarked) could fail without your existing rule. You may need to adapt the pattern or set up redirects for the old style.
Managing lots of redirects and rewrites can become a headache. I'd advice some attention to your url name spacing. And documenting them.
I reserve first level patterns for aliases/shortcuts/campaigns.
//example.com/slug
I'd avoid that for your user profile urls if possible.
You'll ideally want to aim for consistency and have one-one correspondence for URLs(with associated http method) and resources (canonical urls).

Create a rewrite rule in .htaccess for php file

I am very new to .htaccess, I am having some problem with file action.
Example:
http://www.domain.com/upload_pic.php?action=save_pic
I wrote the .htaccess rule like
RewriteEngine On
RewriteRule ^sabc([a-zA-Z0-9!##$-_]*)$ upload_pic.php?action=$1
I want the desired result like:
http://www.domain.com/sabc/save_pic
How can I get the desired result, please correct my .htaccess line.
Change it to:
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
The .+ will capture one or more characters after the / and be captured into $1
There are several other guides on the web already, but to understand it in better way as you are beginner #AMY, I have writen this for you. Hope this will work for you.
Most dynamic sites include variables in their URLs that tell the site what information to show the user. Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7.
http://www.domain.com/upload_pic.php?pic_id=7
The problems with this kind of URL structure are that the URL is not at all memorable. It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). Search engines and users alike get no useful information about the content of a page from that URL. You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). It's a fairly standard URL - the sort you'd get by default from most CMSes. Compare that to this URL:
http://www.domain.com/sabc/7/
Clearly a much cleaner and shorter URL. It's much easier to remember, and vastly easier to read out. That said, it doesn't exactly tell anyone what it refers to. But we can do more:
http://www.domain.com/sabc/user-navnish/
Now we're getting somewhere. You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. It's an easy URL to remember and to pass to another person.
Unfortunately, the last URL cannot be easily understood by a server without some work on our part. When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "upload_pic.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^sabc/?$ upload_pic.php [NC,L] # Handle requests for "sabc"
A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example).
The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:
RewriteRule - Tells Apache that this like refers to a single
RewriteRule.
^/sabc/?$ - The "pattern". The server will check the URL of every
request to the site to see if this pattern matches. If it does, then
Apache will swap the URL of the request for the "substitution"
section that follows.
upload_pic.php - The "substitution". If the pattern
above matches the request, Apache uses this URL instead of the
requested URL.
[NC,L] - "Flags", that tell Apache how to apply the rule. In this
case, we're using two flags. "NC", tells Apache that this rule should
be case-insensitive, and "L" tells Apache not to process any more
rules if this one is used.
# Handle requests for "sabc" - Comment explaining what the rule does (optional but recommended)
The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules.
RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1
Hope this will be helpful for you to understand URL rewriting #AMY.

Using a backreference at the beginning of a pattern in an apache rewrite rule

I'm currently translating a website from english to spanish. I want to use a rewrite rule that can put the current language at the beginning of the url, just after the domain. Example:
current url without rewrite rules:
http://www.example.com/sample_subdirectory/?language=spanish
objective:
http://www.example.com/spanish/sample_subdirectory/
At the time of this writing, I have all my php files rewritten as if they were subdirectories (such as example.com/something.php to example.com/something/) so there's no problem there. My objective though is to see if there's a way to rewrite the url so that instead of displaying the language GET variable, it displays "/spanish/something/something_else/etc/"; The goal is to not have to do an individual rewrite rule for every url just for the spanish translations, but rather append that /spanish/ subdirectory towards the beginning of the url. I tried to figure out how to use the metacharacters but I don't think I was doing it right. Here's what I tried (I'm pretty sure this is done so wrong :/ )
RewriteCond %{QUERY_STRING} ^id=(spanish)$
RewriteRule ^/spanish/(.*)$ /$1?id=%1
I'm still kinda new at rewrite rules, I only know basic stuff, I'm definitely not a web server administrator so I wouldn't know how to implement what I'm going for. Hopefully it's possible to do this. Ironically, I could have probably finished typing simple rewrite rules instead of spending an hour trying to do this but oh well :P
Based on the example you give above, the following should do what you want:
RewriteCond %{QUERY_STRING} ^language=spanish$
RewriteRule ^/(.*)$ /spanish/%1?
The RewriteCond causes the RewriteRule to be run only when the query string is "language=spanish".
The RewriteRule saves the directory from the original URL and appends it to the directory "/spanish/". The ? at the end removes the query string.
Thanks to bobdye's insight, I realized I had to remove the condition altogether and tweak the RewriteRule to look like this:
RewriteRule ^/subdirectory/spanish/(.*)$ /subdirectory/$1?language=spanish
Note: /subdirectory is a folder inside a virtual server (localhost) that I have running on my laptop.
As you can see, I had to place the /spanish/ directory in the pattern and whatever subdirectory was under that I would use as a back reference to the actual link, then I would just append the GET variable language=spanish to the end of the link. I'll still need to test to make sure that this works for any number of subheadings under /spanish/ but this definitely solved my problem.

How to make multiple product or news sites without making tons of PHP files

i was thinking, regular site have just few subpages like:
-home
-about
-contact
And things like that, so in general you usually get below 10 .php files, but what if somebdy want to make CMS which generate new site for each news, right it is possible to make new php file for each of it but its unproductive.
I also find a way that i can just make something like "yourdomain.com/news/index.php?id=24" and just get data from mysql where ID = 24 but i need to display name of news in URL
So my question is how to make something like speciic URL for each news for example: www.yourdomain.com/news/name-of-new
without making tons of php files and with specific description, keyword for each of them
The best answer here is to adopt a MVC programming method with one entry point that you define. Let me show a very basic example that will not involve using a pre-made framework. Basically you will have one entry point (usually the index.php file) and then for the most basic example one folder with modules for each page.
You have to program the index.php so that according to a parameter it will load the correct page file. So far this is just what you suggested with the ?id=27 thingy.
Now let's make it a little piper. Create a .htaccess file next to your index.php one. A .htaccess file can define rules for your whole website. Here you need to make some rewrite rules.
A rewrite rule is basically something that will tell your website to act like if the user asked index.php?id=27 whereas he asked yourdomain.com/news
So type first this code to enable the rewrite rule engine
RewriteEngine On
RewriteBase /
Then make a simple rewrite rule like this one
RewriteRule ^/(.*)$ /index.php?page=$1 [L]
Please keep in mind that you must use relative links otherwise this so far implicit redirection will show up in the browser nav bar.
There is a plenty of way to make rewrite rules, check Htaccess rewrite rule on the internet to find one that suits to your architecture.
EDIT: for preventing your rewrite rule to run when the requested url is a file (like an image) use these directives before the rewrite rule line
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Mod_rewrite question mark not appearing in output url

I'm trying to setup a simple mod_rewrite to redirect some open cart links which get broken when an import is done. Essentially the pretty urls for information pages don't go to where they should so im planning on overwriting them using .htaccess
Anyway I have a setup like so:
RewriteEngine On
RewriteBase /myshop.co.uk/
RewriteRule ^about-us/why-shop/?$ index.php?route=informationin/formation&information_id=7 [L]
So as I understand the following
www.myshop.co.uk/about-us/why-shop
will display the page contents of
index.php?route=information/information&information_id=7
However when I test this its only returning
www.myshop.co.uk/index.php
Everything after and including the ? appears to be ignored, first I thought I had to escape it but after reading various posts it doesn't seem like I need to.
Also while I'm here, is it possible to specify a RewriteBase which includes sub directories? For example
RewriteBase /development/myshop/
Everything after and including the ? appears to be ignored,
No that's correct it is appending full URL as per your rule. Only thing is that since you're not using R flag this redirection is happening silently.
If you add R flag in your rule then you'll notice full URL being displayed in your browser:
RewriteRule ^about-us/why-shop/?$ index.php?route=informationin/formation&information_id=7 [L,R]
Reference: Apache mod_rewrite Introduction

Categories