1

I have an address which is /blog?url=article-1

Is there a way to just remove the ?url= part and replace it with a forward slash?

So, in the end, the URL should look like /blog/article-1

I have tried many htaccess routes and none seem to work.

I am using Windows and I am building a simple blog.

The URL works and everything but I just need it to look like /blog/article-1 instead of /blog?url=article-1.

What I have mainly tried when searching on Stack Overflow is this code:

RewriteRule ^edit/id/([0-9]+)/?$ edit.php?id=$1 [NC,QSA,L]

Changed the values in it with no luck, what would I change the above code to? Or is the above code entirely wrong for what I need?

2
  • 2
    Show us what you've tried.
    – John Conde
    Commented May 15, 2015 at 14:33
  • I have done this with IIS but what are you using linux, windows? and I think this question belongs to stackoverflow not here Commented May 17, 2015 at 23:51

1 Answer 1

1

The URL works and everything but I just need it to look like /blog/article-1 instead of /blog?url=article-1

This should be done in your web application, not .htaccess. It is a common mistake (at least a common question) to attempt to do this part in .htaccess, as it will result in multiple external redirects on every request - which is not desirable.

The only time you do this in .htaccess is when you need to correct old URLs that have been indexed and linked to. Essentially a canonical redirect.

RewriteRule ^edit/id/([0-9]+)/?$ edit.php?id=$1 [NC,QSA,L]

What you should then use .htaccess for is to internally rewrite the virtual (pretty) URL to the real URL. That is what this directive is doing.

So, the basic process is:

  1. User clicks on "virtual" link (a URL) on your website: /blog/article-1
  2. Directive in .htaccess internally rewrites the request to the real file that handles the request: /blog.php?url=article-1. (Although the file extension is missing on the URLs in your question.) Since this is an internal rewrite (as opposed to an external redirect) the URL in the address bar does not change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.