: Use .htaccess to remove a subdirectory from a URL when it in turn has a subdirectory Could anybody please tell me how to rewrite http://example.com/forum/topic/topic-name to http://example.com/topic/topic-name
Could anybody please tell me how to rewrite
example.com/forum/topic/topic-name
to
example.com/topic/topic-name
I do not want to simply rewrite example.com/forum to example.com/ for every URL, but: example.com/forum/A/... to 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)/(.*) // [R=301,L]
RewriteRule ^forum/(user)/(.*) // [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/ 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/ example.com/topic/
(of course example.com is replaced by my domain)
but it's not working.
More posts by @Holmes151
2 Comments
Sorted by latest first Latest Oldest Best
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)/(.*) // [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.
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/ example.com/topic/
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.