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.
Related
I want to remove .php from the link in form's action attribute.
the link is:
localhost/shopproject/MassUpload/create_excel.php/createExcel.
I've tried using .htaccess from other questions, but I find only the way to remove .php at the very end of the link e.g
localhost/shopproject/MassUpload/create_excel.php/createExcel.php
to
localhost/shopproject/MassUpload/create_excel.php/createExcel.
I wonder how can I make the link to:
localhost/shopproject/MassUpload/create_excel/createExcel
If it isn't possible is there a better way so that I can hide the usage of create_excel.php from a user?
Thank you for the help
if you ONLY need it for this specific form...
RewriteEngine ON
RewriteRule ^create_excel/createExcel$ create_excel.php/createExcel.php
or if you have a lot of different forms using something like this...
RewriteEngine ON
RewriteRule ^(.*)/(.*)$ $1.php/$2.php
Please note, you probably need to add the correct path that your live website uses.
I think something like this (if you added in proper paths) may be able to help you. I haven't done rewrites in a while, but this would say /anything/whatever is the same as /anything.php/whatever.php
so this would be like saying that /create_excel.php/createExcel.php can be accessed through create_excel/createExcel
If this doesn't work, or you can't create something similar in .htaccess, please try the following I suppose...
create_excel/createExcel/index.php
This would allow you to use site.com/create_excel/createExcel/ as a link, because index.php is automatically used (and not displayed in URL bar) when you set up a folder for this.
If you are using some sort of third party library or system, I highly recommend not creating a custom folder like this.
So, I ran into an issue with query strings and rewriting them to seem "prettier"
My website is at: localhost/admin/ so when uploaded to my server it is also under the /admin directory, and the .htaccess file looks like this:
RewriteEngine On
# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*)$ /admin/index.php?$1 [NC,L]
Unfortunately, my design for the admin panel all of the pages are linked to the index.php file. So when I originally used to request http://localhost/admin/?users it would show a list of all of the users. I wanted to make this prettier so I used the above htaccess rules and came up with http://localhost/admin/users. Two issues arose with this:
The css files within the admin folder under css/ could not be accessed so none of the styles were being applied.
This ?users page contains pagination using the GET parameter in php as well, so it looks kind of like http://localhost/admin/users&page=1 and used to look like http://localhost/admin/?users&page=2.
So basically what I wanted to do was make the code look more like this: http://localhost/admin/users/page/1. Unfortunately I cannot hard code the users parameter in because I have other sections like "websites" and I will also have a edit users page with id's etc. So I was wondering if someone could help me with the best way to approach this problem or how I can fix the above htaccess rules? The only thing that kind of helped was: .htaccess mod_rewrite Unknown number of Variables of a GET form but I am still lost on how to implement it for multiple empty GET variables at the front of the url.
Thanks!
Here's what I do,
First for css part use <base href="domain.com/"> in head section of your pages.
You are redirecting full url to query string of index.php and I am assuming you are retrieving is as $_SERVER['QUERY_STRING'];.
So I suggest you that you change your pagination url to http://localhost/admin/users/page/1.
And after that when you retrieve them in index.php explode the string using explode function,
$arr = explode('/',$_SERVER['QUERY_STRING']);
Inside your $arr you will have all your values which you got them from query string.
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]
Do I have to put every file in a different folder?
like:
about-us/about-us.php
profile/profile.php
etc.
or is there any other automatic solution.
I want to convert
http://sitename.com/about-us/about-us.php
to
http://sitename.com/about-us
You want pretty URL rewriting.
An Apache .htaccess examples from that article:
Pretty URL: /browse/animals-24/cats-76.html
Ugly URL: /browse.php?category=24&subcategory=76
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^browse/[A-Z0-9_-]+-([0-9]+)/[A-Z0-9_-]+-([0-9]+)\.html$ browse.php?category
If you have a directory called about-us, then you could simply create an index.php file within that directory, and by default, your .htaccess file should redirect users to the correct page.
Thus, going to example.com/about-us/ would bring the user to the same page as about-us.php. It would benefit you to do some research about 301 redirection.
http://www.tamingthebeast.net/articles3/spiders-301-redirect.htm
Basically, when Googlebot crawls your website, the last thing you want is for Google to find both copies of the page, one listed as about-us/ and one listed as about-us/about-us.php. Duplicate content is bad, and optimizing your website for search engines is really not at all that difficult to do.
Let's say you have a leaderboard page on your website with 1000 members. Instead of Google finding all leaderboard.php?user=Whatever, it would be a good idea to block that page from being accessed by Google, or else you will result in hundreds of unwanted archived pages on their search engine.
You might also want to make sure your website can be accessed either by www.yourwebsite.com or by simply yourwebsite.com, BUT NOT BY BOTH (without being 301 redirected).
Hope that helped. Happy programming!
EDIT: If you try renaming your about-us.php file to simply index.php, that would be your quick fix. Depending on your .htaccess configuration, I'm willing to bet that would be your easy fix.
I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]