Apache: mod_rewrite: generate URL based on passed PArameters - url

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

Related

URLs in some web applications (short URLs)

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

How to mod_rewrite a query parameter to a path folder name?

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.

hide url directory

I have a website:
www.mydomain.com/subfolder/subfolder/index.php
How could always hide the directory names from the url. I mean always hide the 2 subfolders' names from the url as the page changes.
Example:
These urls:
www.mydomain.com/subfolder/subfolder/index.php
www.mydomain.com/subfolder/subfolder/about.php
www.mydomain.com/subfolder/subfolder/contact.php
...
Becomes:
www.mydomain.com/index
www.mydomain.com/about
www.mydomain.com/contact
...
I want to use the last mentioned urls for requesting these pages, too, without typing a horrible long url.
you can find some good hints when you will look for mod_rewrite or mod rewrite in htaccess.
If you have access to the Rewrite engine, you can use a simple rewriting pattern similar to this:
RewriteRule ^/(.*)$ /subfolder/subfolder/$1.php

SO like URL with mod_rewrite

I want to get a URL scheme exact like SO's question URL.
Like http://stackoverflow.com/questions/6035363/so-like-url-with-mod-rewrite
this page can be accessed with http://stackoverflow.com/questions/6035363
but then it redirects to the http://stackoverflow.com/questions/6035363/so-like-url-with-mod-rewrite
I need to do the same.
My page will be in the root directory named article.php
the url will be like
article.php?id=1&title=some_text
i want it to be accessed with [any of these will do]
acticle.php/id/1/some_text
or
acticle/id/1/some_text
or
acticle/id/1&title=some_text
[Sorry i really dont know about mod_rewrite so i read some question and googled some cheat sheets but none of them worked :(
So looking for some copy-paste solution]
It's best if you divide and conquer the URL to reverse engineer it.
So a few key points:
6035363 is the important part of the URL as it's the question ID.
Everything else is arbitrary SEO, friendly URLs
Having multiple URLs of the same page will hurt SEO, so stick to one URL format
With that said, article/id/some_keyword_text is the format I suggest. The following RewriteRules will handle it.
RewriteEngine On
RewriteBase /
RewriteRule ^article/(\d+)/?$ test/article.php?id=$1 [L]
RewriteRule ^article/(\d+)/([\w-]+)/?$ test/article.php?id=$1&title=$2 [L]
The first rule redirects article/id with an optional trailing slash. The second rule redirects article/id/some_keyword_text with an optional trailing slash. some_keyword_text may only contain alphanumeric, underscore, or dash characters.
Note: The redirection for URLs of format article/id happens at the page level. So this is not a RewriteRule, but logic in the page that redirects when title is not set. I'm guessing a 301 Redirect to ensure point #3.

how to simplify the url

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!

Resources