1

Could anybody please tell me how to rewrite

http://example.com/forum/topic/topic-name

to

http://example.com/topic/topic-name

I do not want to simply rewrite http://example.com/forum to http://example.com/ for every URL, but: http://example.com/forum/A/... to http://example.com/A/... if and only A = 'topic'.


UPDATE 2: The provided solution suddenly no longer works :( Below is the full content of my .htaccess file:

order allow,deny

# Baidu:
deny from 91.197.129.0/24
deny from 185.10.104.0/24
deny from 188.129.143.0/24
deny from 113.119.37.227
deny from 123.125.71.0/24

# Ahrefs:
deny from 37.58.100.0/24
deny from 209.222.8.0/24
deny from 173.199.115.0/24
deny from 173.199.116.0/24
deny from 173.199.120.0/24

# Trendiction:
deny from 144.76.23.0/24

# Random
deny from 27.153.128.0/17
deny from 27.152.0.0/13
deny from 121.204.0.0/14
deny from 120.40.0.0/14
deny from 117.24.0.0/13
deny from 222.76.0.0/14
deny from 220.249.160.0/19
deny from 120.32.0.0/13
deny from 110.80.0.0/13
deny from 220.160.0.0/15
deny from 220.162.0.0/16
deny from 61.135.192.0/18
deny from 77.88.0.0/18
deny from 77.91.224.0/24
deny from 87.250.224.0/19
deny from 92.241.182.0/24
deny from 93.158.128.0/18
deny from 95.108.128.0/17
deny from 119.63.192.0/21
deny from 123.125.64.0/18
deny from 178.154.128.0/17
deny from 180.76.0.0/16
deny from 182.118.0.0/16
deny from 193.47.80.0/24
deny from 213.180.192.0/19
deny from 220.181.0.0/18
deny from 27.153.128.0/17
deny from 27.152.0.0/13
deny from 121.204.0.0/14
deny from 120.40.0.0/14
deny from 117.24.0.0/13
deny from 222.76.0.0/14
deny from 220.249.160.0/19
deny from 120.32.0.0/13
deny from 110.80.0.0/13
deny from 220.160.0.0/15
deny from 220.162.0.0/16
deny from 220.181.0.0/16
allow from all


<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On

RewriteRule ^forum/(topic)/(.*) /$1/$2 [R=301,L]
RewriteRule ^forum/(user)/(.*) /$1/$2 [R=301,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

(below is outdated)

UPDATE 1: My current .htaccess file:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I tried Stephan's suggestion: adding Redirect /forum/topic/ http://example.com/topic/, now my .htaccess file becomes:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Redirect /forum/topic/ http://example.com/topic/

(of course example.com is replaced by my domain) but it's not working.

1

You won't actually need any complicated regular expressions for this particular use case: a straightforward Redirect statement -- rather than a RewriteRule -- can be used to redirect an entire directory to another directory. Use the following line:

Redirect /forum/topic/ http://example.com/topic/
5
  • Thanks, but this does not work. When visiting example.com/forum/topic/[topic-name], I am not redirected to example.com/topic/[topic-name].
    – Khue
    Dec 21 '15 at 18:09
  • @Khue, have you actually tried it? It should redirect everything in that sub-directory like you want. Dec 21 '15 at 19:51
  • @StephenOstermiller: I tried adding Redirect /forum/topic/ http://example.com/topic/ at the end of my .htaccess currently containing <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule \.(jpeg|jpg|gif|png)$ /public/404.php [NC,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>.
    – Khue
    Dec 21 '15 at 19:55
  • @Khue What are you redirected to? As mentioned, this directive should do as you require. Try clearing your browser cache (if you've previously tested with permanent redirects then these will have been cached by the browser). The other problem you might have is if you have existing RewriteRule (mod_rewrite) directives that are conflicting. mod_rewrite will execute before mod_alias (Redirect), regardless of their order in .htaccess. (Bit of an aside, but the above Redirect directive is a "temporary" redirect, so you might want Redirect 301 ...)
    – MrWhite
    Dec 21 '15 at 19:55
  • @w3d: Thanks. Please see my previous comment (added just a few seconds before yours).
    – Khue
    Dec 21 '15 at 19:57
0

Since you are already using mod_rewrite to internally rewrite the request, you will need to use mod_rewrite to perform this redirect, otherwise there will be a conflict.

Try the following before your existing rules:

RewriteRule ^forum/(topic)/(.*) /$1/$2 [R=302,L]

This should redirect* /forum/topic/topic-name to /topic/topic-name. (*I assume you wanted an external redirect and not an internal rewrite?)

Change the 302 (temporary) redirect to 301 (permanent) when you are sure it's working OK.

8
  • Hi. Suddenly the rewrite is no longer working (I upgraded the forum to a new version, which is only a minor upgrade, a while a go, but I don't believe this is the cause). Could you please suggest a way to check?
    – Khue
    Jan 4 '16 at 13:27
  • Presumably everything else is still working OK? Does the .htaccess file look OK, with the above redirect at the top, before your other directives? Has the URL changed? Presumably your forum uses these .htaccess directives, so the .htaccess file is otherwise working OK?
    – MrWhite
    Jan 4 '16 at 17:37
  • Yes everything was working ok. The .htaccess file is the same, no URL has changed :(
    – Khue
    Jan 4 '16 at 19:58
  • And when you say it's not working, presumably it is not doing anything? ie. You're not redirected? Try temporarily adding any gibberish to the start of your .htaccess file just to make sure it is being read - this should trigger a 500 Internal Server Error. Otherwise, add your .htaccess file to your question so we can have a look.
    – MrWhite
    Jan 4 '16 at 20:33
  • Yes, "it's not working" means that I am not directed (unlike before). Putting some gibberish gives an internal server error. I have added the full .htaccess file, please have a look at it.
    – Khue
    Jan 4 '16 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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