1

hmm I have a problem with my .htaccess, I wanted to rewrite this:

http://domain.com/a/thread-103518.html#103518

to this

http://domain.com/a/103518

In other words... I want to delete the "thread-" and everything after the "." The /a/ must be variable... there are other forums with /bla/ and /code/

Do you have a clue for me? Thank you

My .htaccess right now:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index.php
RewriteRule ^index.php/?(.*) /$1 [L,R=301]

1
  • Please clarify which URL is the one "coming in from the wild", and which is your desired output of .htaccess. All too often I see requests to "rewrite [dynamic format] URI to [SEO format]", when the desired action is actually the opposite. If thread-103518.html is a real page, and 103518 is SEO format, you want to rewrite the latter to the former. Commented Jun 26, 2014 at 15:37

1 Answer 1

1

Add this to the .htaccess file in your DOCUMENT_ROOT

RewriteEngine On
RewriteRule ^([^/]+)/thread-(\d+) $1/$2? [DPI,L,R]

Tested in Apache 2.2 and 2.4 :)

This assumes that mod_rewrite is both installed and activated for htaccess files. If you are not sure, to check if mod_rewrite is installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

Sign up to request clarification or add additional context in comments.

8 Comments

thank you for the fast answer... the redirect work but now I have a DC issue. My .htaccess RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php RewriteRule ^index\.php/?(.*) /$1 [L,R=301] RewriteRule ^([^/]+)/thread-(\d+) $1/$2? [DPI,L] domain.com/a/thread-103518.html#103518 AND domain.com/a/103518 are now possible, but I only want the second URL
I thought you said http://domain.com/a/thread-103518.html#103518 should redirect to http://domain.com/a/103518. Not understanding, that's not the question anymore?
the link now works but you can enter the site with domain.com/a/thread-103518.html#103518 and domain.com/a/103518 . on a click at the domain.com/a/thread-103518.html#103518 Link, I want a redirect and URL rewrite on domain.com/a/103518
I think I understood. I added an R in the flags [DPI,L,R]. Now it will redirect and the url in the browser will also change. Let me know if this is what you're looking for. :)
perfect! with the R Tag it works perfect. thank you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.