-1

I'm creating a blog, and would like to use the title of each entry as the URL. The blog is pulling from a database and has code in a single php file to display the blog entry.

I would like any url like, domain.com/blog/this-is-the-title.html to redirect to domain.com/blog/index.php BUT, keep the URL in the browser bar as the original url.

EDIT...

domain.com/blog/anything-that-is-here.html

should redirect to domain.com/blog/index.php

But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.

I hope this makes sense.

Hoping this is something that can be accomplished in.htaccess.

Thanks! Rick

Rick
  • 441
  • 1
  • 3
  • 15
  • Does this answer your question? [URL rewriting with PHP](https://stackoverflow.com/questions/16388959/url-rewriting-with-php) – user3783243 May 09 '20 at 00:51

2 Answers2

0

You are searching for the rewrite function from Apache.

Something like this should work for your case:

RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1

An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".

Furkan
  • 288
  • 1
  • 8
  • Thank you Furkan. Should this also work if the url is something like domain.com/blog/this-is-the-title.html (It doesn't seem to be workiing) – Rick May 09 '20 at 00:52
  • What error message do you get? Maybe add a test echo to your php file to see if it gets executed at all – Furkan May 09 '20 at 00:56
  • No error is shown. It just doesn't redirect. I put a simele "Hello" in index.php, but nothing. – Rick May 09 '20 at 01:00
  • It won't redirect visible. It will just internally call the index.php while showing the original url with .html in the browser. Is the .htaccess and your index.php in the same directory? What's your server configuration? Local server on Windows or remote Server? – Furkan May 09 '20 at 01:01
  • No, the index.php I'd like to use is also in the /blog/ directory. The .htaccess I'm editing is in the root. – Rick May 09 '20 at 01:04
  • Please try putting them both in the blog directory – Furkan May 09 '20 at 01:05
  • I might have another issue going on. Let me troubleshoot my .htaccess file and try again. Thank you for your help! – Rick May 09 '20 at 01:49
  • No problem! Please mark my answer if it helped you :) – Furkan May 09 '20 at 01:51
  • Not to totally change things up, but I think this approach will work better with my current .htaccess setup. If a user lands on www.domain.com/blog/any-title-at-all.html, it will redirect them to www.domain.com/blog_item.html. – Rick May 09 '20 at 02:16
  • Well it is difficult to help you, if we don't really understand what you are trying to achieve. In your post you wanted a rewrite to your index.php. Now you don't want to use php at all and just want a file mapping? – Furkan May 09 '20 at 15:24
-1

Try using [P]

RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]
Adeel Tahir
  • 203
  • 2
  • 9