I Have a .htaccess file for a blog script which currently generates a number for a blog post based on it's ID in the MySQL database. Is there any way I can change this so that it uses something like $subject as the URL from my PHP file? I have pasted my current .htaccess code below.
RewriteEngine On
RewriteRule ^blog-([0-9]+).html$ index.php?act=blog&id=$1
I hope people can understand what I am trying to describe.
Thanks in advance.
Regards,
Callum Whyte
Using just the subject is probably a bad idea, if you ever change the title, all links to your page (from google, or elswhere) will be broken. I would include both the subject and the idea, that way you'll always be able to match an URL to a blog post:
RewriteEngine On RewriteRule ^blog-[^-]+-([0-9]+).html$ index.php?act=blog&id=$1
This should match everything except a - between blog and the id.
Now you should be able to call, e.g., *yousite/blog-some_title_or_other-494.html* and it will call index.php?act=blog&id=494. This new rule shouldn't break any of your old backlinks either. If it does, let me know and I'll fix it.
Related
I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.
Alright, I usually try to figure things out on my own, but I am stumbled. I am currently making a PHP registration/login/profile system with PHP and SQL. I am 97% finished, but I would like some help with the users' profiles.
I currently have a form where you can search for users. This is a GET form that links to "www.example.com/profiles.php?username=." I would like for it so that I can link it to something like "www.example.com/user/" or "www.example.com/profile/." I would also like the functionality of going directly to the "user/" URL.
I am sure there are rewrite rules to do this, and I have tried many. I still seem to have trouble getting it to work. Any help? Thanks in advance.
Here is my current .htaccess file (doesn't work):
RewriteEngine On
RewriteRule ^user/([^/]*)\.php$ /profile.php?username=$1 [L]
If this htaccess does work, what should I be linking to? Because currently, linking to the "/profile.php?username=" doesn't do anything.
Again, to clarify, I want the output from the GET form to be able to link to www.example.com/profile/USERNAME or www.example.com/user/USERNAME. I would also like for the user to access their profile by typing www.example.com/profile/USERNAME.
I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies.
So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname.
This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.
I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress.
Thanks
Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:
RewriteEngine On
RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]
and then in your contact.php
determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
if not valid you have a couple options:
redir to your default 404 page
issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
if similar company name in the db possibly redirect to that with a note at the top of the page
Yes you can. You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do.
This is an example of what your htaccess could like:
RewriteEngine On
RewriteRule ^contact/([A-Za-z0-9-]+)/?$ contact.php?company=$1 [NC,L]
I have a problem in htaccess file when i want to write a code of url rewriting in my htaccess file. I want to change this url "quotewebster.com/topics.php?topic_id=12" into this
"quotewebster.com/topics/12/"
I write this a code in my htaccess file:
RewriteRule ^topics/([0-9]+)/?$ topics.php?topic_id=$1 [NC,L]
And it is running fine, But a problem arises when i click any topic it goes to the right page and URL also rewrite but when i want to change the topic and again click on any topic then the url previous topic id embed with the new topic id and URL is something like that
"quotewebster.com/topics/12/topics/13/"
I don't understand it why it is happening. Please help me in this case. And sorry for bad english.
Thanks
Your problem is most likely with the way you generate your urls.
Make sure that your links start with a slash:
href="/topics/12"
not
href="topics/12"
For last 4 days I´ve been trying to make a friendly url using .htaccess (mod_rewrite)
I have a some news on frontpage and link redirects to the full article on news_id.php
So mypage.com/local/news_id.php?newsid=37 should take the headline from the "headline" field
in phpmyadmin - for example mypage.com/local/police-stops-girl-fight-at-the-mall
my .htaccess code is
RewriteEngine on
RewriteRule ^local/([^/.]+)/?$/local/news_id.php?headline=$1 [L]
Some tutorials says I also have to edit the php link on frontpage which is
<?php echo $row ['headline']?
...but I've also stumbled on articles that says that I don´t need it and show this should do
it.
I've checked out if mod_rewrite is enabled at the server and it works fine.
Am I close or is this much more complicated than I think?
Previously you were fetching by id. Now you are attempting to fetch by title.
That's a fundamental difference in the way your article lookups will need to occur and what content will be in your urls.
It may be easier to start out with a simple experiment site. Get that working. Then convert your real site over once you've seen how it all works.