php - .htaccess url rewrite rules problems

918

I need some help to write a rewrite rule : I tested many many things but i guess i'm doing something wrong.

I need to rewrite this kind of url: this is the FROM url :

http://website.com/a-section-a/a-section-b/a-section-c/99999-name-name2#

to: this is the TO url :

http://website.com/index.php/newsection/99999-name-name2

I tried many thing but actually i get it:

RewriteRule /index.php/newsection/ \/([a-z]+([-]|[\/]))+

But not working ( rewrite engine ON ).

edit : The url should redirect to the TO page AND rewrite it.

449

Answer

Solution:

A bit unclear on how your rewrite rule should work due to your syntax and the odd placement of{-code-1}, but try using this:

{-code-3}
{-code-4}
{-code-5}
{-code-6} ^{-code-7}/{-code-7}/{-code-7}/(.*)?$ {-code-1}newsection/$4 [R,L,NC]

Just so you understand how it works, the{-code-3} simply tells Apache to turn on the rewrite rule engine in the ruleset. The{-code-4} assures the rule only kicks in of there isn’t a file with the same name. Similarly, the{-code-5} assures that the rule does not kick in if there isn’t a directory with the same name.

Now the actual{-code-6} breaks down like this:

  • Each{-code-7} represents a segment of the URL path. It only matches the letters a-z (case insensitive) & numbers 0-9 as well as the- character.
  • The/ designates each path part like a real URL.
  • The last part of the path is(.*)?$ which will catch anything`.
  • The area past the regex stuff that matches the URL is the redirect destination with$4 matching the last thing captured by the regex stuff.
  • And the[R,L,NC] are Apache rewrite rule flags that equate to:R means redirect,L means last meaning the ruleset stops processing &NC means match the rule with “no case” (aka: case-insensitive).
892

Answer

Solution:

try below code,

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.html HTTP/
RewriteRule ^index.html$ http://www.yoursite.com/ [R=301,L]

People are also looking for solutions to the problem: php - Apache Solr - Unknown Sort Order

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.