-1

I I have this website, with this structure: http://example.com/-/?id=1234. Now, I want to edit this link to: http://example.com/-/1234.


So, I put an .htaccess in http://example.com/-/; there is content:

RewriteEngine On
RewriteRule ^/?-/([0-9]+)$ /index.php?id=$1

Unfortunately, it always shows: Error - 500.


What is wrong with my .htaccess?


My question is not duplicated with any existed question ...

16ctt1x
  • 311
  • 6
  • 21
  • Search before asking http://stackoverflow.com/questions/990392/htaccess-rewrite-to-redirect-root-url-to-subdirectory http://stackoverflow.com/questions/21147556/use-htaccess-to-rewrite-urls-in-a-subdirectory-only – Lokesh Pandey Aug 18 '16 at 13:18
  • Did you double check the configuration ? – Lokesh Pandey Aug 18 '16 at 13:21
  • HTTP Error `500` or `505`? - they mean *very* different things – CD001 Aug 18 '16 at 13:22
  • `^/?-/` doesn't really make sense to me. Why is that `?` there? – Elias Van Ootegem Aug 18 '16 at 13:22
  • Did you follow this apache tutorial ? https://httpd.apache.org/docs/current/howto/htaccess.html – Lokesh Pandey Aug 18 '16 at 13:26
  • If your .htaccess file is in a sub-directory called `/-/` then prefixing it with that hyphen is going to prevent the match working - it's made against the URL relative to where the .htaccess file resides. However, that shouldn't cause a 500 error; that's an internal server error meaning that you've probably got some invalid syntax somewhere. – CD001 Aug 18 '16 at 13:28
  • There's nothing in what you've shown us that would cause a `500` error ... the issue is somewhere else I think; what you've got there does actually work (though the leading `/?` is probably unnecessary). – CD001 Aug 18 '16 at 13:29
  • @YeuSeChia Check the error logs. These logs can provide valuable context related to any code failures or other potential causes of a site failure. And notify us once again – Lokesh Pandey Aug 18 '16 at 13:33
  • @EliasVanOotegem the `?` means match the preceding character 0 or 1 times. Basically it makes the `/` optional to the pattern. You can have it or not have it, it doesn't make a difference. _Edit:_ Just looked at your profile, you probably already knew that. Doh! – Henders Aug 18 '16 at 13:56
  • @Henders: I understand regex, it just doesn't make sense in a rewrite rule, that's all I'm saying – Elias Van Ootegem Aug 18 '16 at 13:57
  • @EliasVanOotegem yep, sorry. See my edit on the comment! :P It would make sense though afaik because a .htaccess file would omit the leading slash but if the rule was in the VirtualHost context, the leading slash would be present and would adjust the regex as we are matching the start of the string. See the [Per-Directory Redirect](http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule) – Henders Aug 18 '16 at 14:00
  • Possible duplicate of [URL RewriteRule in .htaccess for index.php query parameters](http://stackoverflow.com/questions/21854952/url-rewriterule-in-htaccess-for-index-php-query-parameters) – Louis Aug 18 '16 at 14:07
  • Have a look at the duplicate, it is the exact same thing, that is turning query parameters (after the ?) into route parameters (between slashes) – Louis Aug 18 '16 at 14:08
  • Who cares about `php` or `$_GET`, this is routing from the server level, i.e. apache. You want the query parameters `?1234` to be used a route parameters `.../1234` so yes it is a duplicate – Louis Aug 18 '16 at 17:18

5 Answers5

0

this is htaccess for the sub folder

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /foldername
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
Junaid
  • 555
  • 6
  • 23
  • but it is working for me on godaddy. please check it again properly. – Junaid Aug 18 '16 at 13:40
  • @YeuSeChia 404 error code exists when it does not match a file that exists on the server in the new location specified by the rewrite rule. So check it double check it all – Lokesh Pandey Aug 18 '16 at 13:44
  • yes it try to match the route you have no parameter in your function so it gives error 404. try to correct your route first. ?id=1234 is not a route request but /1234 for this request you should define the route – Junaid Aug 18 '16 at 13:48
0

You can use the following rule in /-/.htaccess

RewriteEngine on
RewriteRule ^/?([0-9]+)$ /-/?id=$1 [L]
Amit Verma
  • 38,175
  • 19
  • 80
  • 104
  • 1
    Check your server error log for info about the 500error, it's not due to the rewrite rule. – Amit Verma Aug 18 '16 at 14:00
  • ... which is the conclusion I came to about half an hour ago ;) Only gotcha could be a recursive rewrite loop I suppose - though that should `508`. – CD001 Aug 18 '16 at 14:04
0

This should work:

RewriteEngine On
RewriteRule ^-/([0-9]+)$ /-/?id=$1

It rewrites correctly on my test server ( Apache/2.4.6 ), here is the rewrite trace log (with redirect turned on for clarity):

[rewrite:trace2]  init rewrite engine with requested uri /-/1234
[rewrite:trace1]  pass through /-/1234
[rewrite:trace3]  add path info postfix: /var/www/html/example.com/- -> /var/www/html/example.com/-/1234
[rewrite:trace3]  strip per-dir prefix: /var/www/html/example.com/-/1234 -> -/1234
[rewrite:trace3]  applying pattern '^-/([0-9]+)$' to uri '-/1234'
[rewrite:trace2]  rewrite '-/1234' -> '/-/?id=1234'
[rewrite:trace3]  split uri=/-/?id=1234 -> uri=/-/, args=id=1234
[rewrite:trace2]  explicitly forcing redirect with http://example.com/-/
[rewrite:trace1]  escaping http://example.com/-/ for redirect
[rewrite:trace1]  escaping id=1234 to query string for redirect id=1234
[rewrite:trace1]  redirect to http://example.com/-/?id=1234
Dusan Bajic
  • 8,851
  • 3
  • 29
  • 35
0

According to what you've written your directory structure is this:

  "/" (root)
   |
   |___ "-" (directory)
         |
         |___ .htaccess
         |___ index.php

However the RewriteRule is this:

RewriteRule ^/?-/([0-9]+)$ /index.php?id=$1

With the directory structure you've specified that rule will never match unless you're requesting an url like http://www.example.com/-/-/123 (note 2 levels of hyphen). This is because the path that .htaccess uses is relative to where the file is and you've stated that it's in a directory called "-". The leading slash will not jump the URL matching up to root, and because you've allowed it 0 or 1 times (?) it'll just be ignored so, in effect, it's looking for a directory called "-" from inside a directory called "-".

Then if you look at where your rewrite is actually going, it's not going to /-/index.php but to /index.php (the root of the domain); the leading / will take effect on the rewrite.

The rule almost makes sense if your index and .htaccess files are in the docroot:

  "/" (root)
   |
   |___ .htaccess
   |___ index.php

... and that "-" directory doesn't actually exist but it just being used as a rewrite trigger in the URL.

In which case your RewriteRule should be:

RewriteRule ^-/([0-9]+)$ /index.php?id=$1

HOWEVER none of this will cause an HTTP 500 error - you have a further problem somewhere that you've not shown us.

CD001
  • 7,661
  • 2
  • 22
  • 27
-1

According to your request, this is your server structure:

"/" (root)
|
|___ "-" (directory)
|
|___ .htaccess
|___ index.php

Before you make a .htaccess file, please check your server configuration.

RewriteEngine On
RewriteRule ^([0-9]+)$ ?x=$1
Community
  • 1
  • 1