Rewrite rule for this - .htaccess

Please help me for this mod_rewrite result
how i can get using .htaccess
http://www.domain.com/artist.php?userid=2&name=username
to
http://www.domain.com/saquib
please help

You can easily rewrite requests like /saquib internally to /artist.php?name=saquib by using a rule like this:
RewriteRule ^[a-z]+$ artist.php?name=$0
But the problem with your example is the user ID that cannot be obtained from the requested URI path. So unless you use a path that also contains the user ID (e.g. /2-saquib), the user names need to be unique to identify the users just by their user name.
By the way: You should think about using a unique path prefix as some kind of namespace for your users like /users/….

Related

.htaccess dynamic links rewrite engine

I need your help creating some links using mod_rewrite.
I have some pages like:
register.php
login.php
And have the code for them:
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^login/?$ login.php [NC,L]
My problem is with "dynamic" links I have since I can't get them working.
For exemple I have links like:
index.php?id=news
índex.php?id=news&article=2
How can I transform those links into:
/news/
/news/article_name
And I have some products (that could have the same name in the same category) but with different ID's like:
índex.php?id=products&p=30
How can I change it to
/products/product-name
After this, is it possible to "generate" an unique name? Since I would like not set in the link the unique ID like products/45342/product-name?
What are the changes I need to make to my code to work with those links?
For example I have links like:
To clarify, you must first change the links in your application to be of the form /news/ or /news/article_name (but see below). You then rewrite these "pretty" URLs back to the underlying filesystem path.
So, to rewrite /news/ back to index.php?id=news you can do something like:
RewriteRule ^(news)/$ index.php?id=$1 [L]
Using the $1 backreference just saves typing. Only use the NC flag if this must be a case-sensitive match, but note that this potentially creates duplicate content, so you must specify the canonical URL in some other way (eg. rel="canonical" link element). For the same reason, only make the trailing slash optional if this is a specific requirement.
However, it's not possible to rewrite /news/article_name back to index.php?id=news&article=2 (I assume that should be i, and not í, as in your question?) since the article ID (ie. 2) is not present in the source URL. You need to include the ID in the source URL (or make the article_name unique and a key in your lookup). It would be more usual to create a URL like /news/2/article_name (which is what StackOverflow does), which can be easily rewritten. The article_name in the URL is purely for users (and indirect SEO). In which case you could rewrite this like so:
RewriteRule ^(news)/(\d+)/ index.php?id=$1&article=$2 [L]
This will rewrite /news/N/<anything> to /index.php?id=news&article=N (where N is 1 or more digits).
However, since it rewrites <anything> you should also implement a redirect in your application when the non-canonical article_name is accessed. (Which again, is what StackOverflow does.)
And I have some products (that could have the same name in the same category) but with different ID's like: índex.php?id=products&p=30
How can I change it to /products/product-name
The same principle as mentioned above applies here also.
After this, is possible to "generate" an unique name?
You can generate this "unique name" in your application, not .htaccess. Build you URLs in your application etc.
Since I would like not set in the link the unique ID like "products/45342/product-name" ?
As mentioned above, either your product-name is unique, and behaves like your id. Or you incorporate the unique ID in the URL - this is the far more common approach, offers greatest flexibility and is less prone to error. A "short" URL like /products/45342 will redirect you to the correct canonical URL.

GET parameter becomes the page name after rewrite

I have an url that looks like:
/platforms.php?platform_id=xxx
where xxx is a number
I'm rewriting the URL inside the php application. So, for example the above url would look like:
/xbox/ or /playstation/
Now in .htaccess I have:
RewriteRule ^([^/]+)/$ platforms.php?platform_id=$1 [L,QSA]
However when I go to a platform page the GET url becomes /xbox/ or /playstation/ , instead of xxx.
Any pointers would be appreciated.
Update:
Hi, the link is not relevant to my question. I've tried to reformulate what I am after for in the example bellow with better details.
Thanks for the answer and sorry for the bad explanation.
Yep, when I said GET url I was referring to $_GET["platform_id"] .
Basically I have an URL called
www.example.com/platforms.php?platform_id=1
In the above example $_GET['platform_id'] = 1.
In the actual php aplication I have a function (let's call it make_link ), with which I make the above URL output like:
www.example.com/xbox/ (since 1 is the id of the xbox platform)
Now in httaccess I also need a rewrite rule that will make accessing the URL work.
So I have :
RewriteRule ^([^/]+)/$ platforms.php?platform_id=$1 [L,QSA]
This does make the rewrite work in the terms that I can access
www.example.com/xbox/
However on the newly accessed page, if I get $_GET['platform_id'], the value for it is xbox/ .
Thanks,
In a RewriteRule, $1 is a variable backreferencing the first regular expression (in your case: ([^/]+)).
So, whatever text forms that part of your URL is what will be stored in $1.
If you wanted to use the consoles' IDs, you'd have to make these IDs part of your URLs. If you don't want that, but if you want your pretty URLs to reflect the names of the consoles, you'll have to rewrite the query part of platforms.php?platform_id=$1 in your .htaccess file.
Instead of querying for IDs (?platform_id=$1), you'll have to query for the consoles' names, e.g. ?platform_name=$1.
Edit:
In your PHP file, you'd then use $_GET['platform_name']

Htaccess redirect and rewrite

I'm trying to get below URL to do a redirect / rewrite
http://www.domain.com.au/posting?id=44404
Below is what is required:
http://www.domain.com.au/state/category/job_title
Is there a way to do this with PHP MySQL Joomla application?
This is using custom component so I have to retrieve state, category and job_title information from MySQL database. Is there a way to insert them into htaccess or recognise so that it reflects on the URL field instead?
Thanks
If you can write a script to get from /state/category/job_title to an id, then you can use a RewriteMap. You can only define a map in either the server config or a vhost config, not in an .htaccess file.
Say you had a perl script that parsed out the state, category, and job_title, did some database lookups, then output 44404 or whatever the id is. Then you can define the mapping like so:
RewriteMap jobmap prg:/path/to/your/script.pl
Then in the htaccess file in your document root, you can use the map:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /posting?id=${jobmap:$1/$2/$3}
If you wanted to enumerate every state/category/job_title combination, you could also use a text file or a dbm file. Or if you have mod_dbd loaded and configured, you can also use a SQL select query
Another alternative is to write some kind of php script and route everything through that. It would essentially be doing what the script.pl example would do, parse out the state/category/job_title fields, and do the proper database lookup to obtain an id. You could then include the posting script and hand over the id.
This is what you are looking for:
http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html
but, as you redirect, your id gets lost
You'll have to write all the codes to replace each id with the needed category state and job title in the .htaccess file, I dont think you can retrieve through mysql and use them to replace in the htaccess file.

How to change a url with htaccess

i have this url:
merchantstore.php?merchant=100&product=208
that i want to convert to:
/merchant-store-name/product-name
where merchant-store-name replace ?merchant=100 and product-name replace &product=208
How do i do that in htacess file.
I'd suggest you to use a PHP-based approach. In the .htaccess:
RewriteRule (.*) switchboard.php?orig_uri=$1
This captures the entire requested uri, and kind of forwards it to a central switchboard. Then in the switchboard.php you have access to the requested uri, which you can explode() along '/' signs, then look up the id-s associated with the names in you database.
$components = explode('/', $_GET['orig_uri']);
list( $merchant_name, $product_name ) = $components;
// get merchant id and product id from your database
// and serve suitable content by include-ing:
include 'merchant.php';
This is a simple method, and rather easy to scale. It also has the added advantage that no mod-rewrite magic is required.
Don't forget to add proper error-handling.

help regarding dynamic redirect rule in htaccess

I need ur help for given subject.
I am playing with htaccess rules first time in life.
here is the scene -
i want to redirect the urls -
http://www.abc.com/var1
http://www.abc.com/var2
to follwing urls -
http://www.abc.com/index.php?u=var1
http://www.abc.com/index.php?u=var2
In this case the values var1 & var2 can be anything (i.e. string which will contain only alphanumeric characters.)
One more thing -
I want to skip this rule if the url is -
http://www.abc.com/SKIPME
Please help me to write this rule!
Regards,
Shahu!
You would be better off defining a URI schema that tells you when something WILL be rewritten. For example...
http://www.abc.com/site/var1
http://www.abc.com/site/var2
That way, you can ensure you only ever apply the rule if the psuedo "site/" directory is browsed and not affect any other URI. This is the rewrite rule based on the above schema.
RewriteEngine on
RewriteRule ^site/([^/\.]+)/?$ index.php?u=$1 [L,NC,QSA]
Any other address, other than "/site/.../" would be unaffected by this rule, which means you don't need to worry about setting some addresses to be avoided. This keeps things as simple as possible.
You don't have to use "site" - you can use whatever name fits your purpose.

Resources