Need help for my url

Hi can i ask some help please, I tried to make pretty url this is the firs time that i will do this,

I have this url > http://localhost/mysite/admin/view.php?id=8

I want to make it pretty url as what I have seen in the net. I tried to put this on my htaccess


RewriteEngine On

RewriteRule ^([a-zA-Z0-9]+)$ view.php?id=$1
RewriteRule ^([a-zA-Z0-9]+)/$ view.php?id=$1

but it doesn’t work, it always show the url like this


in my javascript I did this

window.location.href = "http://localhost/mysite/admin/view.php?id="+theId;

Thank you in advance.

Hi jemz,

I know you know better than to use code like that! Oh, well.

First, decide upon what format you want your pages to be linked as. Since your code shows two different formats (which do not match your code), may I recommend going back to the drawing board and use your page titles (or some other database field which is UNIQUE) for the links? If you really want to use admin/view/8/ as the format (OMG!), then, after complaining about the directory level change caused by the trailing /, I’d tell you to use /([0-9]+)/$ as the regex (because it will only capture the value of the id field).

If you wish to use ^([a-zA-Z_]+)$ to capture the title (where you only use letters and _'s substituted for spaces), then use the above as your regex and use view.php?title=$1 as the redirection.


Full discusstion:

If you find that you need other characters, be careful NOT to use reserved characters (




I think my first post is wrong to have forward slash after,…I think this is what I mean,
just like the sitepoint

I read your link in the

And I hope I understand it.please correct me if I’m wrong.

RewriteEngine on
RewriteRule ^([a-zA-Z_]+)$
mysite/admin/view.php?id=$1 [L]

I don’t know how to call it in my javascript,

window.location.href = "http://localhost/mysite/admin/view/"+myvalue;

so that if ever I click it I get this kind of url,example


Thank you in advance.

Hi jemz,

Thank you for correcting your first post (trailing /) as that has a major impact on the mod_rewrite code.

As for SitePoint’s code, it’s changed during my membership period every time SitePoint has changed the forum software. Currently, I like their t/need-help-for-my-url and but understand that the following /106763/3 denotes the ID of the thread and the number of the post within the thread. Those make it easier for the forum software to ignore the thread’s title (need help for my url) and, thus, eliminates the possibility of duplicate title entries (something I had to caution my client and enforce with code and a database “unique” specification on the title field). Please view to see the links just using the article titles as links (modified to remove spaces and allow LIMITED punctuation - all that is allowed in a URI) simply because the titles are more descriptive and memorable for his website visitors.

As for your code, I must assume that it’s in the DocumentRoot (mysite; because you link internally to mysite/admin/). If that’s the case, using the start anchor and no / in the RewriteRule’s regex will prevent your code from working.

For simplicity, I’d move your mod_rewrite code to the admin directory and remove the mysite/admin from the redirection. Also, your regex needs to match your URI (for the admin directory) so the regex must match view/8 and that certainly cannot be done with letters and underscores.

Leaving the mod_rewrite in the DocumentRoot (localhost), your code should simply be:

RewriteEngine on RewriteRule ^mysite/admin/view/(\d+)$ mysite/admin/view.php?id=$1 [L]

…would do exactly what you’ve asked for (the \d+ is shorthand for one or more digits).

To do something like SitePoint’s URIs (mysite/admin/view/{garbage_characters}/{digits}), use

RewriteEngine on RewriteRule ^mysite/admin/view/[^/]+/(\d+)$ mysite/admin/view.php?id=$1 [L]

… where the {garbage_characters} would be discarded.

To do something like (using the unique title field from the database for the link), use:

RewriteEngine on RewriteRule ^mysite/admin/([-a-zA-Z0-9_&'!\…]+)$ mysite/admin/view.php?id=$1 [L]

… where ([-a-zA-Z0-9_&'!\…]+) includes all letters, digits and ALLOWED ASCII punctuation.

Finally, WHY would you “link” to the view.php script using JavaScript (my understanding of this window.location.href directive is that it MUST be used before any output to the browser)? I use PHP to create my <a href=…>Link info</a> links within a script and populate the href value with my preferred link format, i.e., the title with spaces replaced by _'s and illegal characters removed rather than id=123456.

Whew! This is a long reply with a LOT of information (and options) so I hope I didn’t overwhelm you.




Thank you for the quick response…I appreciated it :smile:

mysite and admin are folders and view is php file which resides in admin folder. also mysite resides in


The reason I use window.location.href is because I’m clicking some element in the document like the div,and I did not use anchor tag.

so when I click to a div this will redirect to http://localhost/mysite/admin/view.php?id=3

this is the js code inside my view.php

function show(theid){

To have pretty url or clean url…I follow as what you have suggested.

I have two lines of code only in my .htaccess

RewriteEngine On
RewriteRule ^mysite/admin/view/(\d+)$ mysite/admin/view.php?id=$1 [L]

when I click on the div the url shows like this


then I click another div


I’m expecting the url to be like this when i click,…Please correct me if I’m wrong in the format.


but it did not work. it only shows like this


I tried to manually enter this in url


It says

The requested URL /mysite/admin/view/7 was not found on this server.

Thank you in advance.

Hi jemz,

Two things:

  1. Please define your DocumentRoot. For my response(s), I’ll assume it’s mysite but recommend that you use your httpd.conf to include the conf/extras/httpd-vhosts.conf file which will then include mysite as a locally hosted domain (at least after you add it to your C:\Windows\drivers\etc\hosts file). As noted before, I will normally place my .htaccess file with my mod_rewrite directives in the DocumentRoot.

1.5. You DID enable mod_rewrite in your httpd.conf, didn’t you?

  1. YOU must define (and create) links in the format you wish to see (apparently this is mysite’s admin/view/7). Therefore, your window.location.href should be the INTERNAL URI (admin/view/7, NOT the EXTERNAL http://localhost/mysite/admin/view.php?id=3 as mysite/admin/view.php becomes the requested {REQUEST_URI}. Note: I believe that you need the path in your window.location.href.

Your use of my code (where it the .htaccess located? Remove the start anchor and see what happens) is problematic because the DocumentRoot is wildly all over the place (at least in my mind … because you’re not using a virtual domain for mysite).

[quote]when I click on the div the url shows like this


then I click another div


Of course it does! That’s exactly what you’ve specified in your JavaScript.

[quote]I tried to manually enter this in url


It says

The requested URL /mysite/admin/view/7 was not found on this server.[/quote]

Ergo the question about enabling mod_rewrite (or confirming that it is working by some other test.

My mod_rewrite tutorial ( discusses configuring the Apache files (I left the virtual host setup out - too much of a side step to cover that, too) AND testing that mod_rewrite is enabled.


Problem 1 - Have you confirmed that mod_rewrite is enabled? See my tutorial for more information on how to configure Apache and test that mod_rewrite is, indeed, enabled.

Problem 2 - Where is your .htaccess located? If you can’t define DocumentRoot sufficiently, put it in the admin directory and remove everything up to admin in your mod_rewrite code (including the start anchor). That implies that the admin’s .htaccess would have:

RewriteEngine on RewriteEngine On RewriteRule view/(\d+)$ view.php?id=$1 [L]

Problem 3 - in your view.php script, use a RELATIVE link to view.php with the appropriate id value … AS YOU WANT TO SEE IT. Remember, your job when creating mod_rewrite code is to convert the {REQUEST_URI} to something Apache can serve. That means that you need to change your JavaScript to:

function show(theid){ window.location.href=('view/'+theid); }

You’re welcome (‘in advance’).



Yes I enabled it.

I also tried to test if it is working like in your example in
the test.html and test.php and it shows “This is the PHP file”.

I tried to follow your steps,

First, I enable this virtual host in httpd.conf

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Second, I add this to C:\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf

 <VirtualHost *:80>
        DocumentRoot "C:/wamp/www/mysite"
        ServerName mysite
        ErrorLog "logs/"
        CustomLog "logs/" common

Third, I add this to the host file, C:\Windows\System32\Drivers\etc   mysite

Then I restarted my wampserver,

In my js script I use this


I also tried this one

My .htacess I placed it here “C:\wamp\www\mysite”

RewriteEngine On

RewriteRule ^mysite/admin/view/(\d+)$ mysite/admin/view.php?id=$1 [L]

when I tried to manual input in the url


The requested URL /admin/view/2 was not found on this server.

I also move the htaccess to the admin folder as you suggested but not working.

I don’t know why it is still not working… :disappointed:

Hi jemz,

It looks like your virtual host is setup correctly - congratulations!

The error I see is that you’re including your (virtual) domain name in the regex (which can ONLY see the {REQUEST_URI} as well as the redirection. In other words, DELETE the “mysite/” in both!

As your RewriteRule stands, it will NOT be matched (because of the mysite/ at the start of your regex) and, even if it were matched (by deleting the mysite/), you would be requesting mysite/admin/view.php?id={whatever} from your mysite domain (you don’t have a mysite subdirectory within your mysite domain, do you?).

With that explanation, deleting “mysite/” from the regex AND redirection should allow your redirection to work. Remember, though, after using the R=301 flag to view and confirm proper redirection, you should remove it … unless you want to see the redirection.

Your test url is fine.



do you mean to change my .htaccess like this

RewriteEngine On

RewriteRule ^admin/view/(\d+)$ admin/view.php?id=$1 [L]

but still error Not found url…please correct me if I am wrong

Thank you in advance.

Hi jemz,

That looks okay to me so there must be another problem lurking in the background:

  1. Confirm DocumentRoot location
  2. Confirm mod_rewrite is enabled (and TEST that it’s working, too - see for “How To” info on both).
  3. Add the R=301 flag to VIEW the redirection. This will allow you to confirm (one way or the other) whether the redirection is working (the problem may be in the $_GET[‘id’] of view.php). Remember, though, remove the R=301 to “go public” with your code.



I just grab the id like this


    $theid = $_GET['id'];

Then I passed this to my js by putting it to the hidden field value.



I tried to create simple project to perform this pretty url,and it works fine,I can grab the i.d and echo it.something like this http://pretty/admin/view/4,and it works fine,I think there is something problem in my original project, I found out in the firebug I have this errors and some other external file like jquery.js

“NetworkError: 404 Not Found - http://localhost/mysite/admin/css/bootstrap.min.css
“NetworkError: 404 Not Found - http://localhost/mysite/admin/js/jquery.js


From the code snippets shown, you can’t get from the PHP variable $theid to the JavaScript variable theid without an intermediary step. Why not make the JS assignment:

window.location.href='admin/view/'+<?php echo $theid; ?>; OR window.location.href='admin/view/'+<?=$theid?>;

Your last post (#12) socks me as you seem to have gotten to mysite/admin/view/4 and, presumably, the mod_rewrite will send it to admin/view.php?id=4.

When mod_rewrite starts redirecting (as displayed by using the R=301 flag for testing) the server’s root information, that is indicative of a problem within Apache and a RESTART of Apache is required to clear this error. After the restart, everything should work properly. While that’s a simple thing for you to do on your own computer (localhost), it often takes some arm twisting of JUNIOR hosting support staff to restart a shared server - but it IS necessary (I’ve not discovered any other recovery mechanism).




I mean I created another project under www folder,called prettyurl then I also created vitulal host for this I named ther servername “pretty”,

Then I tried this manually in the url http://pretty/admin/view/4 , and It works it did not say url not found,…so I tried to go back to my original projects http://mysite/admin/view/4 ,It not say url not found but the error is my js files,css etc.

“NetworkError: 404 Not Found - http://localhost/mysite/admin/css/bootstrap.min.css
“NetworkError: 404 Not Found - http://localhost/mysite/admin/js/jquery.js

I apologize I could not get what you mean by this.I don’t know how to use R=301 for testing,

RewriteRule ^admin/view/(\d+)$ admin/view.php?id=$1 [L,R=301]

Once you’re happy that this code works (it will display admin/view.php?id=4 if the requested URI is admin/view/4), then remove the ",R=301 to hide the redirection.



This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.