• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

.htaccess redirection in wrodpress

Hello Experts
I'm really not good in .htaccess I'm moving a site to a word-press site ,
I want to redirect the old URLs to the new ones  301 redirection
My htaccess is
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [R=301,L] # 301 redirect, and stop processing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window


old URLs look like this
http://site.com/blog/index.php?id=how-to-be-good
New URLs should be like this
http://www.site.com/blog/how-to-be-good

so what I want to do is just remove  "index.php?id="  from the old URL

I tried many things but nothing works for me

this the last one
RewriteRule ^index\.php\?id=(.*)$ /blog/$1 [R=301,L]

Please Help and explain your code
Many thanks
0
honestman31
Asked:
honestman31
  • 8
  • 7
1 Solution
 
grahamnonweilerCommented:
You can use either of the below rewrite rules, and should be at the top of your rewrite rules:

RewriteRule ^/?blog/([a-zA-Z0-9_/-]+)$  http://www.yourdomain.com/blog/index.php?id=$1 [L,NC]

Open in new window

~ or ~
RewriteRule ^/?blog/(*)$  http://www.yourdomain.com/blog/index.php?id=$1 [L,NC]

Open in new window

0
 
honestman31Author Commented:
Unfortunately  , none of them work

Also from the code itself you forgot that I want "index.php?id="   to be removed
0
 
grahamnonweilerCommented:
Because you are redirecting (not just rewriting) the "index.php" will appear.

Also, if "blog" is a directory then you must place the htaccess there

The following will rewrite (not redirect) in the "blog"

RewriteRule ^/?([a-zA-Z0-9_/-]+)$  ./index.php?id=$1 [L,NC]

If "blog" is not a directory then the below will rewrite at the root level:

RewriteRule ^/?blog/([a-zA-Z0-9_/-]+)$  ./index.php?id=$1 [L,NC]
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
honestman31Author Commented:
I added the code after     RewriteBase /


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/?blog/([a-zA-Z0-9_/-]+)$  ./index.php?id=$1 [L,NC] 
RewriteRule ^(.*)\.html$ /$1 [R=301,L] # 301 redirect, and stop processing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window


all giving me the same result

http://www.site.com/blog/?id=how-to-be-good

The desired result is

http://www.site.com/blog/how-to-be-good

P.S  blog is not a directory
Further more I want to use 301 redirect so I tell the search engines the old URL has been permanently  moved to the new URL
0
 
grahamnonweilerCommented:
Your htaccess is removing "index.php", try:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/?blog/([a-zA-Z0-9_/-]+)$  ./index.php?id=$1 [R=301,L,NC] 
RewriteRule ^(.*)\.html$ /$1 [R=301,L] # 301 redirect, and stop processing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window

0
 
grahamnonweilerCommented:
I think what you are trying to do is change the URL on the same website, which you can not do using an .htaccess on the same website.

Meaning:

If you have 2 domains "mydomain_a.com" and "mydomain_b.com" then you could "rewrite" the URL at "mydomain_a.com" to redirect to the new URL style at "mydomain_b.com"

But:

You can not make "mydomain_a.com" rewrite and redirect to itself!
0
 
honestman31Author Commented:
I tried your code and still getting the same result

http://www.site.com/blog/?id=how-to-be-good

The desired result is

http://www.site.com/blog/how-to-be-good


It is the same domain , the whole idea is to let the search engines know about the new URLs  and give them the same ranking ( or page ranking )  as the old ones

And yes, it is one domain

I was able to  change those urls ending with .html    by using this

RewriteRule ^(.*)\.html$ /$1 [R=301,L] # 301 redirect, and stop processing

and it works fine  so I can use redirection to the same domain
0
 
grahamnonweilerCommented:
Redirect and rewrite are two different things!

rewrite leaves the original URL showing in the address bar

redirect changes the URL in the address bar.

If you entered http://www.site.com/blog/how-to-be-good in to your browser, then the rewrite I gave you would NOT change the URL as you have shown it.

Something else is then "redirecting" the rewritten rule.
0
 
honestman31Author Commented:
ok , so I want redirect

all what I want is
URLs like this
http://site.com/blog/index.php?id=how-to-be-good

to be redirected [301 ] to URLs   like this

http://www.site.com/blog/how-to-be-good

the key here is how to remove "index.php?id="  from the URL
0
 
grahamnonweilerCommented:
Now I understand.

You will need to write a PHP script to handle that as you can rewrite/redirect based on the query string.

The PHP script has to take the query string and make it in to a URL and then send that to the browser using a header.

Unfortunately though this will not help you with SEO, and the script will not send a 301 redirect - it will send a 302 (temporary).
0
 
honestman31Author Commented:
I want to do it thru .htaccess  ,  
If I was able to do it to remove the .html    from the URL then should be a way to remove   "index.php?id="  from the URL
0
 
grahamnonweilerCommented:
You are confusing what you did with the HTML script name and the "query string".

The query string is the part after the ? in the URL.

RewriteCond %{QUERY_STRING} ^id=([a-zA-Z0-9_/-]+)$
RewriteRule ^index\.php$ http://mydomain.site/blog/%1?  [R=301,L,NC]

Open in new window

0
 
honestman31Author Commented:
You code works great, That's what I want , I still wondering why you said cannot be done using .htaccess and wanted me to use PHP

and this one is even better ( shorter )
RewriteCond %{QUERY_STRING} ^id=([a-zA-Z0-9_/-]+)$
RewriteRule ^index\.php$ blog/%1?  [R=301,L,NC]

Open in new window

I never been  satisfied about my .htaccess knowledge
Thank you for your effort
0
 
grahamnonweilerCommented:
Be careful though, the rewrite I gave you will rewrite anything that has a query string containing "id=" and will always add the "blog" portion.

That is why I recommended a PHP script - becuase you could be far more selective in what you "redirect".

Anyway, its working for you, and that's what counts.
0
 
honestman31Author Commented:
lucky wordpress uses   ?p=   not   ?id=
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now