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.
Related
I am currently using rewrite rules in .htaccess.
For example:
RewriteRule camp/johndoe /camp/2020/SuperLanding.html?c=summer2020&d=2&i=149
RewriteRule camp/janedoe /camp/2020/SuperLanding.html?c=summer2020&d=2&i=150
But I have a lot of them and every time I add another person I have to make a change to .htaccess.
My setup is a website using HTML pages driven with PHP and MySQL.
How can I use a table in my MySql DB to drive these pretty permalinks automatically?
I have seen this question How do I create permalinks that are database driven?. It does not seem to answer how to use the DB.
I got it to work as follows:
I changed my PHP to accept both the parameter i for number ID and parameter p for pretty name.
When there is no i GET parameter it will look for a p parameter and use it to look up the ID in the DB.
In .htaccess I changed my rule to:
RewriteRule ^camp/(.+)$ /camp/2020/SuperLanding.html?c=summer2020&d=2&p=$1 [NC]
Anything coming in with text after camp/ will be forwarded to /camp/2020/SuperLanding.html?c=summer2020&d=2and the text will be added to the p parameter
I am trying to make a custom blog CMS. Each blog post has an ID in my mySQL database.
Basically I need to rewrite:
domain.com/category.php?id=4
to
domain.com/4
But I only want this to work if the ID (in this case 4) exists in the database because I don't want every single number to be rewritten.
Can someone tell me how I can get around with this?
You can use the usual file manipulation functions(fwrite, fopen etc..).
Or you can use Mod_rewrite and PHP. Check this : http://culttt.com/2011/11/16/how-to-make-vanity-urls-using-php-htaccess-and-mysql/
I'm trying to redirect a bunch of pages from one domain to another (not all the pages, just part of them).
The URL of a page is domain.com/?p=ID
ID is always a number.
I'd like to redirect all pages with IDs under 2000 a new domain, say domain2.com/?=ID
How can I do it? I'll probably have to use REGEX patterns, but I'm not that savvy when it comes to REGEX.
Thanks,
Roy
It is probably possible to do with a regex but regex is not really suited to doing ranges like that. Another way to do it could be to use a RewriteMap in your Apache conf like so:
RewriteMap examplemap txt:/path/to/file/map.txt
RewriteRule ^/?p=(.*) ${examplemap:$1|/?p=$1}
Your /path/to/file/map.txt file would then contain something like:
1 http://domain2.com/?p=1
2 http://domain2.com/?p=2
3 http://domain2.com/?p=3
.
.
2000 http://domain2.com/?p=2000
If the entry is not found in the map file then it should default to the existing domain because of the part after the pipe in the RewriteRule. This might seem like an overkill but it gives you the finest level of control over each redirect.
The above code has not been tested but hopefully it explains the principal. See the Apache docs for more information on using RewriteMap.
We send out a newsletter that has URLs in it. Rather than having foreign URLs directly, they all come to our website and then redirect to the outside world.
Right now the redirects are all done with HTML files. My goal is to have them all done with redirects in the .htaccess file. So I want to have the person who is entering all of this data enter it all through the movable type GUI.
My questions:
Is there is plug-in for movable type that already does this?
If not, is there a good template for creating a movable type that allows one to records in the MySQL database?
Thanks.
This could be done in the standard Entry interface in MT. Just dedicate a blog for these redirects. You could make the EntryTitle the redirect and have the EntryBody be the full URL (or use Custom Fields). Then just create an .htaccess template that loops through all the entries.
<mt:Entries lastn="0">
Redirect /<mt:EntryTitle dirify="-"> <mt:EntryBody>
</mt:Entries>
The way I would do this is to create a custom field for the outside URL.
Then I would populate the .htaccess file with something like:
Redirect /
In the above coding, I'm looping through the latest 999 entries and I'm checking if the custom field with the tag 'EntryDataMyCustomField' is filled out.
If it is filled out, then I redirect / to the URL from that custom template.
This is like redirecting say:
/234 to whatever URL you may think of, like say:
http://en.wikipedia.org/wiki/Main_Page
I have a link, let's say: http://site.com/profile.php?id=1 ....In a normal mode, with a normal rewrite I'd have something like: http://site.com/profile/1 . But,...What I want is.....how can I get from the database the username that belongs to the user with the id 1 and make the url http://site.com/profile/FinalDestiny ?
Thanks,
You could use the RewriteMap function of mod_rewrite, but you have to store your id-username pairs either into a text file or a DBM file-based database. Keep in mind that to use this directive you have first to declare it inside httpd.conf, so access and write permissions to httpd.conf are required.
If your users are stored in another database (MySQL or such) I'd suggest you use a text file and simply overwrite it any time an user is added or deleted.
If you've got enough users to make using a DBM a sensible choice you can either try to use PHP's DBA functions or make a text file as above and use Apache's utility httxt2dbm to convert it into a database (via exec() or such).
RewriteMap also allows using an external program (say, a PHP CLI script) to return the URL mapping, but it seems like overkill...
You want to make a rule which rewrites http://site.com/profile/FinalDestiny to http://site.com/profile.php?username=FinalDestiny and then do a database query in profile.php to find the id of the user with that username.