Tauleshwar Thakur
Tauleshwar Thakur

Reputation: 39

Redirecting specific parameter to a subdirectory using .mod_rewrite

I recently added WordPress in a subfolder(blog) on my existing http://localhost/mysite PHP website where I have already rewritten URLs for &_GETS requests, WordPress website works fine when I type http://localhost/mysite/blog/ but when I type http://localhost/mysite/blog it still works but URI is changed to http://localhost/mysite/blog/?id=blog . Any help will be appreciated.

In short

I want http://localhost/mysite/blog

to change into http://localhost/mysite/blog/ using .htaccess

but it automatically changes to http://localhost/mysite/blog/?id=blog

.htaccess

RewriteEngine on


##Rewrite for downloading page##
RewriteRule ^download/([0-9]+) /mysite/download.php?file=$1 [NC]
RewriteRule ^downloadpdf/([0-9]+) downloadpdf.php?file=$1 [NC]


#For sitemap.xml
RewriteRule ^sitemap.xml sitemap.php [NC]


#This section is for forms url rewriting for course

RewriteRule ^([^/.]+)$  index.php/?id=$1 [NC]
RewriteRule ^([^/.]+)/([^/.]+)$  index.php/?id=$1&course=$2 [NC]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)$  index.php/?id=$1&course=$2&stream=$3 [NC]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)$  index.php/?id=$1&course=$2&stream=$3&subject=$4 [NC]


#Custom pages pretty URL's
RewriteRule ^about-us about-us.php [NC]



RewriteCond %{QUERY_STRING}  ^id=read$    [NC]
RewriteRule ^([^/.]+)$ read/index.php [NC,L,R=301]

ErrorDocument 404 /mysite/errors/not-found.php

Upvotes: 2

Views: 61

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Based on your shown samples could you please try following. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON

##Rewrite for downloading page##
RewriteRule ^download/([0-9]+)/?$ /mysite/download.php?file=$1 [NC,L]
RewriteRule ^downloadpdf/([0-9]+)/?$ downloadpdf.php?file=$1 [NC,L]

#For sitemap.xml
RewriteRule ^sitemap\.xml sitemap.php [NC,L]


#This section is for forms url rewriting for course
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/?$  index.php/?id=$1 [NC,L]

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/([^/.]+)/?$  index.php/?id=$1&course=$2 [NC,L]

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$  index.php/?id=$1&course=$2&stream=$3 [NC,L]

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$  index.php/?id=$1&course=$2&stream=$3&subject=$4 [NC,L]

#Custom pages pretty URL's
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^about-us about-us.php [NC,L]

RewriteCond %{QUERY_STRING}  ^id=read$    [NC]
RewriteRule ^([^/.]+)$ read/index.php [NC,L,R=301]

ErrorDocument 404 /mysite/errors/not-found.php

Fixes in OP's attempted code:

  • There are no L flags used for rules so they will be keep going further even their matched condition made a Rewrite, that's what I believe is happening in your url's case.
  • There are no conditions added, so when there are no more conditions then it will catch any kind of uris which is NOT an ideal case, so I have added conditions to it.
  • Also trailing(optional) slashes are missing in left part of regex which I have added them now.
  • Dot was not escaped too, so escaped dot too in rules.

Upvotes: 1

Related Questions