I have a question about short URLs. Let me ask by giving an example.
We have this URL : www.domain.com/controller/function/parameter/?get=1
How can we show this URL like this : www.domain.com/ShortName
For example in joomla cms we have Alias that allows us to shorten the URL even with UTF-8 characters.
How can it be possible in php?
Thank you very much :)
You need to research URL rewriting.
Here is a very detailed article that provides a wealth of information and some examples:
http://corz.org/server/tricks/htaccess2.php
Here is an example.
In your .htaccess file you would enter something like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC]
This translates an unfriendly URL like this:
http://corz.org/blog/index.php?archive=2003-nov
into a friendly and easy to remember URL like this:
http://corz.org/blog/2003-nov
Related
I'm just new to .htacces
I want:
example.com/page.php?page=how-to-learn
changed to:
example.com/how-to-learn
I google'd some and tried but no change url.
That's not how .htaccess work : it won't change your links to display the corrected version, but it looks for a match in the expressions you'll write into it to rewrite the correct parameters.
For example, in your code, your .htaccess sould look something like this :
RewriteEngine On
RewriteRule ^([a-z-]+)$ page.php?page=$1 [QSA,L]
If you write example.com/how-to-learn in your browser, you'll be able to see the content of example.com/page.php?page=how-to-learn.
I advise you to be VERY careful though : as the expression is VERY permissive, you'll have to check in the page.php file that the file if legitimately allowed to be loaded.
I'm doing my first steps with url-rewriting and can't get the following to work:
In my application, a skin can be loaded by applying query parameter ?skin=some_id to any page in the application. I want to change:
http://www.mysite.com/anypage.html?skin=123
into:
http://www.mysite.com/123/anypage.html
but I cannot get it to work.
This is what I currently have in my httpd.conf:
<IfModule mod_rewrite.c>
RRewriteRule (.*)/(.*)?app=(.*)$ %1/%3/%2 [NC,R=301,L]
</IfModule>
Questions:
This isn't working, so I would like to know what I'm doing wrong?
Also with the URL in effect, what is the URL the user enters? http://www.mysite.com/123/anypage.html which "maps" to http://www.mysite.com/anypage.html?skin=123?
And if I want to access the query parameter, do I have to extract it from the actual url (?skin=...) or from the rewritten URL?
Thanks for helping out!
EDIT:
So I have it sort of working doing it like this (helpful tester here):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} skin=(\w+)
RewriteRule ^.*\.html /%1? [R=301]
</IfModule>
This will redirect:
www.some.com/index.html?skin=xyz => www.some.com/xyz
Not quite there yet.
I'd recommend going about skinning your application differently. The way you have it now will create duplicate content issues with search engines because they will see the same content for each page on your site for every skin you have.
That is to say, yoursite.com/dark/about.html would be identical content to yoursite.com/spring/about.html so search engines may have a hard time deciding which version to use. In addition, it seems like it will create extra work for linking to other pages on your site since you will have to create your links programmatically to use the proper path and skin.
I would just have a URL for activating a skin and store their preference in a cookie or in a session and skin the site based on the cookie/session value and only maintain one set of URLs.
Unless you really want the skin to be in the URL, I would shy away from using the URL or query string to indicate which skin to use. Instead have it be a preference attached to an account or stored in a cookie.
Hi:
In my website I found that their url are very simple like:
http://example.com/questions/4486620/randomaccessfile-probelm
And generally the url should like:
http://example.com/questions?xx=4486620&title=randomaccessfile-probelm
That's to say there is no request parameters combined by "&" in the url.
How did they make it? Is there any framework?
UPDATE:
My application works under tomcat.
On an apache web server you can do this using mod_rewrite http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Here is an example along with a tutorial...
The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123
Read more about htaccess And mod_rewrite Tutorial by Blogstorm SEO Blog
RewriteEngine on
RewriteRule ^product/([^/\.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/
RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
Read more here that example by the way is copied from http://www.blogstorm.co.uk/htaccess-mod_rewrite-ultimate-guide/ - assigned kudos!
well i have been use static mod_rewrite rules for some time now like i used to do this
RewriteRule ^Animals\.html$ wallpapers.php?f=Animals&of=0&s=Newest
but now what i want to do is create the URL of the leech pages with keywords... LIKE
preview.php?image=26
preview.php is my leech page & 26 is my imageID(primary key) in the database, which corresponds to a imageName, i want the mod_rewrite to create the page URL using that image name... i have been googling about but could find some usable tutorial... can any one help me learn how to do it?
solved it myself
RewriteRule ^([^/\.]+)/?\.html$ preview.php?image=$1
Thanks to the people that've helped me so far with this, I'm ready for the last step, I think.
I've got my URLs looking like this.
/brochure.php?cat_path=35&name=concrete-intermediate-posts
This is great and finally I just need to know how to turn that URL into this desired URL:
/brochure/35/concrete-intermediate-posts
Just like the Stack Overflow format.
Could anyone help me with the correct .htaccess rule?
Also, if I have other get variables in other sections, will this re-write harm them? (they use different variable names)
Thanks
With mod_rewrite you will rather do the opposite: rewrite a URL path like /brochure/35/concrete-intermediate-posts internally to /brochure.php?cat_path=35&name=concrete-intermediate-posts:
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?cat_path=$2&name=$3 [L,QSA]
The other side, using a URL path like /brochure/35/concrete-intermediate-posts instead of /brochure.php?cat_path=35&name=concrete-intermediate-posts in the HTML documents, would be done with PHP.
I Hope you mean something like this:
RewriteEngine On
RewriteBase /
RewriteRule ^brochure/([0-9]+)/([-a-zA-Z0-9]+)$ /brochure.php?cat_path=$1&name=$2 [L]