Using .htaccess to rewrite clean & custom URL structure - .htaccess

How do i redirect
website.com/about.php
to
website.com/about
Also, is it possible to manually create the appearance of subdirectories using .htaccess?
i.e. website.com/project1.php
to
website.com/projects/project1
Much appreciated!

Yes!
You can use the Apache RewriteEngine module to achieve this end. The documentation is here: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
There are a number of good tutorials around also.
Some examples of rules are:
RewriteRule ^/(\S+).php /$1
RewriteRule ^/(project\d+).php /projects/$1
However, these are just guesses at what you might want to achieve, there are always corner cases. (Also, these are not tested!)
Bear in mind the above two rules would not necessarily be good together as written. Take note in the documentation about setting up the RewriteEngine and using appropriate RewriteRule options.
For example, when specifying many RewriteRules it is common to specify the [L] option so that the RewriteEngine stops rewriting after the rule is applied. Ordering of rules, therefore, can be significant.

Here are your examples putting the .htaccess on your root directory and using mod_rewrite
RewriteEngine on
RewriteRule ^about\.php$ about [R]
RewriteRule ^projects/project([0-9]+)$ project$1.php

Related

how to rewrite SEO URL

I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/files/section.php?id=1
Changed to:
demo.example.com/sample-section
I tried
Since you use .htaccess I assume you are using Apache. Here you'll find all relevant documentation.
First of all you need the mod_rewrite module to be installed (instructions to do so depend on the server's operating system and Apache distribution).
Then, the URL rewrite is pretty simple:
# First of all tell to mod_rewrite to operate.
RewriteEngine on
# Then, as many times you need, tell it on what to operate...
# For example: on files that do not exist. Or leave out RewriteCond to act on all.
RewriteCond "%{REQUEST_FILENAME}" !-f
# ...and what to do.
RewriteRule /sample-section "/files/section.php?id=1" [PT]
RewriteRule /another-section "/files/section.php?id=2" [PT]
The PT (PassThru) flag might be needed in some contexts, otherwise just use [L].

.htaccess redirect whole website structure but leave some of the old structure intact

I have searched but can't quite find the answer to this exact situation:
My website structure is as follows
www.example.co.uk/old-folder1/old-folder2/old-page-name
I needed to redirect the whole structure to:
www.example.co.uk/new-folder1/old-folder2/old-page-name
I successfully did this with:
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
However, if possible for the moment I still want to serve images from the old structure ie.
www.example.co.uk/old-folder1/old-images-folder/old-image.jpg
At the moment the above is being rewritten as:
www.example.co.uk/new-folder1/old-images-folder/old-image.jpg
Which makes sense but this leads me to my question, is there any way of excluding some of the sub directories in 'old-folder1' from the RewriteRule above so that for example www.example.co.uk/old-folder1/old-images-folder/old-image.jpg is still accessible?
I don't have much experience with this but from research I came up with the following but it doesn't work.
RewriteCond %{REQUEST_URI} !^/old-folder1/old-images-folder/.*$
I am beginning to think this might not even be possible with the approach I have taken with:
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
Thanks
Entire contents of the .htaccess file at the moment is:
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/old-folder1/old-images-folder/.*$
RewriteRule ^old-folder1/(.*)$ /new-folder1/$1 [R=302,L]
Try:
RewriteRule ^old-folder1/(?!old-images-folder)(.*)$ /new-folder1/$1 [R=302,L]
HOwever, what's probably happening is that your images are linked using relative URLs, so they may be relatively linked to the "new" images folder.
You should check your links and make sure they're linked correctly to the old path.

How to remove middle part from URL - mod_rewrite

How to rewrite this url "www.domain.com/index.php?route=custom/static/page" to "www.domain.com/page" in htaccess file, basically just want to take out index.php?route=custom/static/ from urls.
I don't know regex so I tried http://www.generateit.net/mod-rewrite/, but it only generates
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
which doesnt remove 'custom/static' from URLs, I tried a few other examples as well but only removes index.php? and doesnt pass variable, any help is appreciated.
Do you know the concept of using mod-rewrite?
In your question you have mentioned to use mod-rewrite to redirect
"www.domain.com/index.php?route=custom/static/page",
Here $_Get['route']="custom/static/page"] $url_parameter=$_Get['route']
to
"www.domain.com/page" [here $_Get['route']="page"],
So now you can mannually add "custom/static/" to the obtained value of $_Get['route']. as $url_parameter="custom/static"+$_Get['route'] //For PHP
Using your mod_rewrite you can fulfill your demands,
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
But if you need out of box solution using .htaccess then I suggest learning "rewrite-engine" instead of using generating tool

Complicated .hatccess rules to convert to web.config

I have the following .haccess rules that I've used for a number of years, however I need them converting to work in a web.config file.
Anybody have a clue where to start?
#Set the mapfiles for the category and product pages
RewriteMap mapfile1 txt:maps/map_categories.txt
RewriteMap mapfile2 txt:maps/map_products.txt
RewriteMap lower int:tolower
#FIX the trailing slash
RewriteRule ^([^.?]+[^.?/])$ $1/ [R=301,L]
#Define the rules for the CATEGORY pages
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$)
RewriteRule ^(?:[^/]+/)?([^/.]+)/?$ product_list.asp?catid=${mapfile1:${lower:$1}}&%{QUERY_STRING} [NC,L]
#Define the rules for the PRODUCT pages
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$)
#RewriteCond %{REQUEST_FILENAME}.asp -d
#RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^.+/.+/([^/.]+)/$ product_detail.asp?prodID=${mapfile2:${lower:$1}}&%{QUERY_STRING} [NC,L]
Very grateful for any help!
Cheers, Chris.
I don't know a lot about .NET web.config files. The last time I used them was four years ago. Therefore I start by explaining what your .htaccess file does, step by step. I focus on what is done and not how, i. e., I don't explain the many expressions like ^([^.?]+, which look like comic figures cursing around.
It's a long answer, so get yourself a coffee.
Let's start with the first three lines.
RewriteMap mapfile1 txt:maps/map_categories.txt
RewriteMap mapfile2 txt:maps/map_products.txt
RewriteMap lower int:tolower
Do you have a RewriteEngine on somewhere in your .htaccess file or perhaps in your host configuration?
RewriteMap sets up a mapping you can use in the rules below like this: ${mapfile1:argument}. You should have two files map_categories.txt and map_products.txt in your maps subdirectories. I can imagine that your application updates these text files. The third mapping converts upper case letters to lower case.
#FIX the trailing slash
RewriteRule ^([^.?]+[^.?/])$ $1/ [R=301,L]
The comment is true. Let's say you have http://example.com/product, this gets redirected (HTTP 301 Moved Permanently) to http://example.com/product/.
#Define the rules for the CATEGORY pages
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$)
RewriteRule ^(?:[^/]+/)?([^/.]+)/?$ product_list.asp?catid=${mapfile1:${lower:$1}}&%{QUERY_STRING} [NC,L]
If you have urls exactly like these: http://example.com/cms, http://example.com/cms/, http://example.com/cms/any_path, and also for vadmin or bathroom-blog instead of cms, then invoke product_list.asp with the corresponding category id. These rules are using the mapping map_categories.txt to map names to IDs, and also allows mixed case, even if the mapping only contains lower case names.
Summary: http://example.com/cms/some_category calls product_list.asp with the category id of some_category.
#Define the rules for the PRODUCT pages
RewriteCond %{URL} ^(?!/(cms|cms/.*)$)
RewriteCond %{URL} ^(?!/(vadmin|vadmin/.*)$)
RewriteCond %{URL} ^(?!/(bathroom-blog|bathroom-blog/.*)$)
#RewriteCond %{REQUEST_FILENAME}.asp -d
#RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^.+/.+/([^/.]+)/$ product_detail.asp?prodID=${mapfile2:${lower:$1}}&%{QUERY_STRING} [NC,L]
This is really strange. The three RewriteCond conditions are exactly the same as the first three above. If this really works, then your predecessor has used some URL Rewriting black magic, for example by exploiting that after a successful rewrite (without redirect), the rules are re-read and again applied to the now rewritten URL. To find more about this, I would need access to your system.
I already know that I didn't see everything: RewriteEngine on is missing, and also the mapping files.
URL Rewriting is one of the subjects which are notoriously hard to get it right without some experimenting. No wonder it is difficult to get working help right out of the box here on Stack Overflow. The problem is that almost always one needs access to the systems involved, because it is impossible to give an answer without some researching the live systems.
As Apache itself says in: http://shib.ametsoc.org/manual/misc/rewriteguide.html#page-header:
The Apache module mod_rewrite is a killer one, i.e. it is a really sophisticated module which provides a powerful way to do URL manipulations. With it you can do nearly all types of URL manipulations you ever dreamed about. The price you have to pay is to accept complexity, because mod_rewrite's major drawback is that it is not easy to understand and use for the beginner. And even Apache experts sometimes discover new aspects where mod_rewrite can help.
In other words: With mod_rewrite you either shoot yourself in the foot the first time and never use it again or love it for the rest of your life because of its power.
Emphasis is mine.
After an hour or so of Googling, I have found that IIS7 supports mapfiles via its rewriteMaps functions. The following two web-pages have given me the solution that I need, and I have created a separate "rewritemaps.config" file to store the ID->URL mappings.
http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module
http://blogs.iis.net/ruslany/archive/2010/05/19/storing-url-rewrite-mappings-in-a-separate-file.aspx
Anyone wishing to convert .htaccess mapfile configurations over to web.config, these two pages will get you going.

PHP Rewrite Rules

The actually URL which my app uses is:
http://site.com/search.php?search=iPhone
but I would like it to be possible to achieve the same with
http://site.com/iPhone
I have no experience of rewrite rules, how can I set this up?
The solution has worked but the new URL is displayed in the address bar. I thought it would have been possible to set this up so that it appears as though the page location is
http://site.com/iPhone
without changing to display
http://site.com/search.php?search=iPhone
Is this possible? Thanks.
Create a file called .htaccess in the root of your website and put this in it.
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*) search.php?search=$1 [R]
Should do the trick.
I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg instead of mysite.com/IPhone use mysite.com/search/IPhone which would work like
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^search/(.*) search.php?search=$1 [R]
This makes it easier to have normal pages that arnt redirected, such as about us or a basic homepage.
As Chris says, this is not PHP but Apache that does this, and whether it works can depend on your hosting setup.
You need to specify something like this in your .htaccess file:
RewriteEngine on
RewriteRule /(.*) /search.php?search=$1
Check also:
mod_rewrite: A Beginner's Guide to URL Rewriting
Module mod_rewrite, URL Rewriting Engine
Rewrite rules aren't part of PHP as far as I'm aware, but Apache (specifically mod_rewrite) or whatever server you're using. For Apache, you need on the server to have a file called .htaccess, and in it put something like:
RewriteEngine on
RewriteRule ^(\w+)/?$ /index.php?search=$1
^(\w+)/?$ is a regular expression - it matches any word of 1 or more characters, followed by a / maybe. So it changes site.com/iPhone into site.com/index.php?search=iPhone. Sound about right?

Resources