0

I am making a site which uses the $_GET method to display certain information on a page. I am having some issues with formatting the URL of the page within my htaccess file, as I want to remove variables and shorten my URL to improve the structure and hierarchy of the URL.

My current URL is as follows:

site.com/project?pn=my_project

The 'pn' variable refers to the project name that is being fetched from the server. What I want to end up with is a way for the URL to be as follows:

site.com/my-project

I want to make sure that 'site.com/home' and other pages similar to it are not effected, and that spaces are not treat as underscores, but as heiphens instead.

How would I go about making these changes?

Please note that my current htaccess file includes the following (I'd like to keep it)

RewriteEngine On
RewriteCond         %{REQUEST_FILENAME} !-f
RewriteRule         ^([^\.]+)$ $1.php [NC,L]

If answers could be explained so that I could rewrite the rule again for another page, I'd be dearly grateful.

Ryan
  • 1,096
  • 2
  • 16
  • 31
  • 1
    You can use [this](http://www.generateit.net/mod-rewrite/index.php) generator if you dont want go deep into rewrite rule – lolbas Dec 28 '14 at 19:17

2 Answers2

3

In the .htaccess file, add this:

RewriteEngine On
RewriteRule ^page/(\d+) /page?referenceid=$1

You can read all about mod_rewrite (the plugin that allows this) here.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • Could you add a link to a reference sheet on this query? I can't quite get it to work. – Ryan Dec 28 '14 at 19:22
  • I'm sorry; I'm not sure what you mean. I already added a link to the official documentation. Can you clarify what you need? – elixenide Dec 28 '14 at 19:23
  • Would you be able to read through the question again as I have rewritten it to make more sense. – Ryan Dec 28 '14 at 21:04
  • I noticed that you have substantially rewritten the question and changed the name of the parameters you want to use. Please do not do that; it breaks the relationship between the question and the answers. Anyway, your existing rule will intercept the request and cause problems. `^([^\.]+)$` matches anything without a `.`, which includes URLs like `my-project`. The `[L]` means that it is the last rule that will run. You need to place this rule above that one and use a `[L]` on it, as well, to stop further processing. – elixenide Dec 28 '14 at 21:24
0

You'll need to use rewrite rules. Assuming you're using Apache, try this:

Place these rules in a .htaccess file inside your document root (if you have access to apache2.conf, that'd be better)

RewriteEngine on
RewriteRule ^/?page(/.*)?$ %{DOCUMENT_ROOT}/page?referenceid=$1 [L]

^/?page(/.*)?$ : matches /page/$1 (the ? mark ensures compatibility with all regexps)

%{DOCUMENT_ROOT} is the location of your document root set either globally or in directory

$1 is the parameter that comes after /page/

[L] means to stop processing any rules following this rule

Just add the RewriteRule after the existing rule you have.

server_kitten
  • 386
  • 3
  • 13
  • As I have no knowledge of htaccess rewrites, would you be able to explain the parts of '^/?page(/.*)?$ %{DOCUMENT_ROOT}/page?referenceid=$1 [L]' so that I can use it again for other pages? – Ryan Dec 28 '14 at 19:25
  • Would you be able to read through the question again as I have rewritten it to make more sense. – Ryan Dec 28 '14 at 21:12
  • I added some details to the answer. Let me know if the rewrite rule worked for you, if not, I can try to improve it so that it does. – server_kitten Dec 29 '14 at 19:57