htaccess -> Rewriting a request - .htaccess

I'm going to try and make this as simple as possible, I have a POST form that sends information called $searchid, I want to redirect it to look like a directory
I want to take the following information: (Server, folder, file, post)
localhost/shorts/profile.php?searchid=12345
And I want to make it look like (Server, fakefolder(id), fakefolder(post))
localhost/id/12345/
Also, is there A way to incorporate the sitename, as all the htaccess ive seen before hasnt had it and it's been showing the entire path from C drive.

In htaccess in the root :
RewriteEngine on
RewriteRule ^id/([0-9]+)/?$ /sports/profile.php?searchid=$1 [NC,L]
This will rewrite
/id/numbers
to
/sports/profile.php?searchid=numbers
and allow you to access the page /sports/profile.php?searchid=numbers using the clean url /id/numbers .

Related

How can I create friendly url redirection made form absolute url's in .htaccess?

I'm writing a program that creates custom friendly urls for websites in .htaccess file. The problem is that I want to create redirect from absolute friendly url to another absolute "real" url, because .htaccess file is placed in root directory and websites are placed in many different child directories.
If the pages were placed in the same folder, I could do something like this:
RewriteEngine On
RewriteRule site site.php
RewriteRule site-two site-two.php
but the pages are placed in different child folders, so the above code doesn't work.
I tried to do something like this but it doesn't work:
RewriteEngine On
RewriteRule http://www.randomdomain.com/sites-dir/site/ http://www.randomdomain.com/content/sites-dir/site.php
RewriteRule http://www.randomdomain.com/sites-dir/site-two/ http://www.randomdomain.com/content/sites-dir-two/site-two.php
I would not like the solution to be similar to 301 redirect because with it, .htaccess refers to another page, but does not save the friendly address in the url bar.
I also want linking pages using a friendly url to be possible.
Does anyone know the solution to this problem?
Thank you in advance for your answer.

Adding a subdirectory to an invisible .htaccess URL rewrite

I wanted to add a subdirectory to my url so it would become easier to read:
Example of what i'd like:
localhost/testwebsite/users.php?firstname=john
should become
localhost/testwebsite/users/john
I tried using the .htaccess mod_rewrite rules and came up with this:
RewriteEngine On
RewriteBase /testwebsite/
RewriteRule ^users/(.*)$ users.php?firstname=$1
What happens why I use that code: it redirects the page successfully, it shows the html of the correct user and it processes the argument correctly. However, all stylesheets, images, scripts, anything with a relative path, could not be found and respond with a 404 message, because of the extra subdirectory added in the new url, I reckon.
Am I doing something wrong? Is there another technique I should be using? Or should I simply make all paths in my project absolute with regards to the root?
You're doing it right. The browser doesn't know that the actual path of the file is different. Use absolute paths or make paths relative to the easier to read URL.

Clean URL rewriting rule

right now my url looks like this:
http://domain.com/en/c/product%2C-product2%2C-product3/82
where last number is category numer.
And im trying to rewrite it and redirect user to url which should look this one:
http://domain.com/82/product-product2-product3
The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting.
Any ideas?
You can use these 2 rules in your root .htaccess for that:
RewriteEngine On
RewriteBase /
RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]
RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]
In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").
If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^url1/?$ url2.php [NC,L] # Handle requests for "url1"

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

URL rewrite conditions in htaccess file

I need some help with .htaccess. I've been using URL rewrites, but not conditions, and I'm pretty sure I need them seeing as I can't get this working.
The URL structure I need is -> forum/catnamehere (which shows the page called catnamehere)
and child structure -> forum/catnamehere/fornamehere (which shows the child page of the cat)
However, somehow I fail to do this, making me believe I need conditions.
You should be able to do this with two rules.
Put your .htaccess file in the forum directory, and add the following:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1&subPage=$2 [QSA]
The first rule will grab stuff like forum/abc123_
And the second rule will grab forum/abc123_/abc123_

Resources