1

So for two days I can not understand why this won't work...:

  Options -Indexes 
  Options +FollowSymLinks
  RewriteEngine On
  RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/$ advice.php?advice=$1&id=$2

Obviously .htaccess is allowed since I am not able to directory open folders, but why doesn't the link work?

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Tsvetilin Boynovski
  • 626
  • 1
  • 5
  • 10
  • Please provide more info about your environment. Which URL do you test, what happens? You may want to test a simple RewriteRule first, like `RewriteRule ^/(.*) http://www.example.com/test1.php?redirected=$1 [L,R]` – PaulH Nov 02 '15 at 09:59
  • Please raise the log level to see if and how Apache processes this rule. Referencing: http://stackoverflow.com/a/14793481/1163786 – Jens A. Koch Nov 02 '15 at 10:02
  • I am testing: domain.com/book/name-of-the-book/2 which should respond to book.php?book=name-of-the-book&id=2 but it says: Not Found The requested URL /book/name-of-the-book/2/ was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. – Tsvetilin Boynovski Nov 02 '15 at 10:15
  • Where are the rules placed? Is it inside server config? Or inside an htaccess file? If the latter, please tell the location of the htaccess file too. – hjpotter92 Nov 02 '15 at 10:27
  • They are placed in a .htaccess files in the /www/(/public_html) directory in a remote server in a hosting company. – Tsvetilin Boynovski Nov 02 '15 at 10:45
  • Options -Indexes +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/?$ /advice.php?advice=$1&id=$2 [L] This now worked, but when I type /advice/newadvice/2 it won't load any styles, but when I open /advice.php?advice=newadvice&id=2 it fully loads as it should. – Tsvetilin Boynovski Nov 02 '15 at 10:52
  • "I am testing: domain.com/book/name-of-the-book/2" - Your directive in .htaccess clearly relates to a different URL format? Any additional information, you should edit and add to your question. – MrWhite Nov 02 '15 at 12:38

2 Answers2

0

Try turning off multiviews so the server doesn't try and search for that as a file. In addition add the conditions so that it knows it's not looking for a real file or directory it's trying to serve and make the trailing slash optional.

  Options -Indexes +FollowSymLinks -MultiViews
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/?$ /advice.php?advice=$1&id=$2 [L]
Panama Jack
  • 24,158
  • 10
  • 63
  • 95
  • Tried that: Not Found The requested URL /advice/test-advice-name/2/ was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. – Tsvetilin Boynovski Nov 02 '15 at 10:47
  • are you 100% positive .htaccess is enabled? Add garbage at the top of the file and if you get a 500 error its enabled. otherwise not and you need to edit the config and change `AllowOverride None to` `AllowOverride All` – Panama Jack Nov 02 '15 at 10:52
  • Options -Indexes +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/?$ /advice.php?advice=$1&id=$2 [L] This now worked, but when I type /advice/newadvice/2 it won't load any styles, but when I open /advice.php?advice=newadvice&id=2 it fully loads as it should. – Tsvetilin Boynovski Nov 02 '15 at 10:53
  • to fix the CSS styles add `` to the head section of your html. – Panama Jack Nov 02 '15 at 10:54
  • the rules work, now just add the css fix I mentioned and let me know. – Panama Jack Nov 02 '15 at 10:55
  • I added right before the tag in the , but it still won't load the styles, though it fully loads when I use the .php url. Also, the <base/> tag doesn't show in the view page source window. – Tsvetilin Boynovski Nov 02 '15 at 11:18
  • @TsvetilinBoynovski Can you provide one example from your code where you link the stylesheet? (The one which does not load). Only put the ` – hjpotter92 Nov 02 '15 at 12:00
  • @TsvetilinBoynovski I'm not sure what you're doing, but either adding the base tag or using a slash in front of your link **will work**. `` http://stackoverflow.com/a/32913668/330987 Make sure it's exactly the way I mentioned. There is no semi colon in there. – Panama Jack Nov 02 '15 at 12:53
  • @PanamaJack Here's an example for the included stylesheet based in header.php, which gets included in each page from my website: – Tsvetilin Boynovski Nov 02 '15 at 16:17
  • @PanamaJack Sorry dude, I had some problem with the deployment in phpstorm, everything's alright now!!! Thank you so much!!! – Tsvetilin Boynovski Nov 02 '15 at 16:19
0

Does your RewriteRule redirect to an existing file? Try this, it works for me

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /test2/test.php
  • http://example.com/test2/.htaccess shows the location of the .htaccess file
  • http://example.com/test2/test.php is a direct link
  • http://example.com/test2/testX4XX opens the same test.php file using rewrite
MrWhite
  • 43,179
  • 8
  • 60
  • 84
PaulH
  • 2,918
  • 2
  • 15
  • 31
  • Works with this now: Options -Indexes +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^advice/([a-zA-Z]+)/([0-9]+)/?$ /advice.php?advice=$1&id=$2 [L] but when I type in the browser /advice/newadvice/2 it doesn't load the styles, it just shows plain text as it comes from the sql and the html, although when I open advice.php?advice=newadvice&id=2 it loads as it should with all the styles. – Tsvetilin Boynovski Nov 02 '15 at 11:22
  • @Tsvetilin Not loading the styles, may be due to the path referencing the styles. may need to be – PaulH Nov 04 '15 at 12:02