skip to Main Content

I’d like to use htaccess to change my URL for my website

URL currently: example.com/index.php?p=my-topic-title
I want to be like : example.com/my-topic-title 

my currrent htaccess file :

    RewriteEngine on

RewriteRule ^p/([0-9أ-يa-zA-Z_.]+) index.php?p=$1 [NC,L]

the result for this code is:

example.com/p/my-topic-title

I want url to be (without (/p/) ) like :example.com/my-topic-title

2

Answers


  1. Chosen as BEST ANSWER

    Thanks, I solved it with:

    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^(([A-Za-z0-9]+[-]+[A-Za-z0-9]+)+[/])$  index.php?p=$1    [NC,L]    
    # Handle blog requests
    RewriteRule ^c/(([A-Za-z0-9]+[-]+[A-Za-z0-9]+)+[/])$  index.php?c=$1   [NC,L]
    # Handle blog requests
    

  2. I get the solution for (spaces) ok but I want to change my URL (example.com/index.php?=my-topic-title) to (example.com/my-topic-title)

    I assuming you have already changed the URL in your application and you only need to redirect search engines/users in order to preserve SEO.

    In which case, you can do something like the following before the above rewrite:

     RewriteCond %{QUERY_STRING} ^p=([^&]{3,})
     RewriteRule ^index.php$ /%1 [QSD,R,L]
    

    This assumes the page title is at least 3 characters. The %1 backreference contains the captured subpattern in the preceding CondPattern. The QSD flag (Apache 2.4+) is necessary to remove the query string from the redirected URL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search