Home RewiteRule not working in .htaccess
Reply: 3

RewiteRule not working in .htaccess

Adan Sandoval
1#
Adan Sandoval Published in 2017-10-27 22:04:31Z

I'm making a website in PHP and I've got to the part where I need to make a news page. I have already written the news.php file and it uses GET to read the particular title of the article to display. That means I end up with an URL like

/community/news.php?title=post-title

And I much rather it to be like

/community/news/post-title

So I looked it up and saw I could do it with the .htaccess file.

Now, my actual .htaccess file looks exactly like this

ErrorDocument 404 /notfound.php

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^community/news/([^/]*)$ /community/news.php?title=$1 [L]

</IfModule>

And he result of going to /community/news/post-title it's an 404 Error

Thank you so much for your help.

Sinisa
2#
Sinisa Reply to 2017-10-27 22:39:58Z

If you work in local environment and your url is like http://localhost/website/community/news/post-title the you should have "website" in "RewriteBase /"

RewriteBase /website
patrickojeh
3#
patrickojeh Reply to 2017-10-28 01:01:20Z

So, assuming your url is example.com - (the QSA flag appends the query string string to the url)

RewriteEngine On

ErrorDocument 404 example.com/notfound.php

# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^community/news/(.*)$ example.com/community/news.php?title=$1 [L,QSA]
Adan Sandoval
4#
Adan Sandoval Reply to 2017-11-23 00:02:23Z

Thanks a lot for your help.

Turns out it was an issue with relative paths. It worked with:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /

# /community/news/post-title -> /community/news.php?title=post-title
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/community/news/([^/]*)$ community/news.php?title=$1 [L]

</IfModule>

(the difference is in the '/'s of the last line)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323167 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO