htaccess rewrite rule blog links - .htaccess

I have been using htaccess to remove rewrite certain URLs, however I am looking at something a little more complicated at the moment.
Our website blog (WordPress) used to have links like this:
​/blog​/postname/1387
However after redoing the website our links are now currently just
/postname
Would it be possible to redirect any uses from ​/blog​/postname/1387 and get rid of the blog and number at the end via htaccess so it just contains the postname? At the moment I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^blog/(.*)/.*$ $1/
</IfModule>
Would love to hear any hints or tips, as this is not actually doing any redirecting, what am I doing wrong?

Let's just do a little cleanup:
<IfModule mod_rewrite.c>
#Turn on the RewriteEngine
RewriteEngine On
#Rewrites are all relative to /
RewriteBase /
#Explicit - If the request is for index.php, do nothing.
RewriteRule ^index\.php$ - [L]
#Conditional – Unless the file or directory specifically exists,
#If the request is for the old style blog URI, redirect to new style and stop.
#Otherwise, redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/.*$ $1/ [R=301,L,QSA]
RewriteRule . /index.php [L]
</IfModule>

Related

Mod Rewrite rule for pagiantion

My home page url is
http://example.com/subsite
My pagination hover link shows
http://example.com/subsite/?page=2
but it redirects back to http://example.com/subsite
whereas it should show and also go to http://example.com/subsite/page/2 which works (if i try manually). Here is my htaccess file looks like,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subsite/index.php [L]
</IfModule>
How to fix the pagination rewrite rule?

.htaccess syntax redirect one domain to another without matching?

What I'm looking to do:
Redirect example.com/(any-number) to example2.com/landing-page
example.com/(any-number) has hundreds of articles all of which end in a number. I'm not looking to match the number on my other site so much as direct people to a single landing page.
Current htaccess looks like this:
`# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress`
Try adding:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^my-site\.com$ [NC]
RewriteRule ^([0-9]+)$ http://my-other-site.com/landing-page [L,R=301]
In the htaccess file in the document root of the "my-site.com" site.

Redirecting a web page to a subdirectory

I want to redirect the page http://www.unived.in/ to http://www.unived.in/aboutus/.
I tried lots of plugins but the page still isn't redirected. In fact I have tried changing .htaccess but it still doesn't redirect properly. Sometimes it displays error 500.
Here is my .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond /home/unived/public_html/wp-content/sitemaps%{REQUEST_URI} -f
RewriteRule \.xml(\.gz)?$ /wp-content/sitemaps%{REQUEST_URI} [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
What do I need to do to make the redirection work?
Read this tutorial to learn more about mod_rewrite: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
It's very detailed, and should help you accomplish your goal.

htaccess mod_rewrite - reroute all urls to index except /admin

I'm setting up a small set of rewrite rules in an htaccess file, where I want every url to go to a index.php file except for /admin which I want to redirect to admin.php. Not very familiar with mod_rewrite or regexp unfortunately.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ admin.php [L]
RewriteRule . /index.php [L]
</IfModule>
This gives me an internal server error (not saying 500). Removing or uncommenting the admin rewrite makes it work.
The conditions need to be applied to the index.php rewrite rule, otherwise it causes a redirect loop. A RewriteCond only gets applied to the immediately following RewriteRule so the rule that routes everything to index has no conditions. Try just rearranging the lines:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^admin$ admin.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Rewrite GET Variable in URL

I am using WordPress's permalink structure %category%/%postname%/ with the following htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The url's take the form http://www.example.com/category/postname/. I have a plugin where if you append ?m=gallery to the URL it will bring you to the posts' gallery. So http://www.example.com/category/postname/?m=gallery brings you to the gallery. I would like to be able to use http://www.example.com/category/postname/gallery/ instead. What do I need to modify in my htaccss to achieve my desired results? I would imagine it would be something like this RewriteRule ^(.+)/gallery$ $1?m=gallery
Edit - Current htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.+)/gallery/$ $1/?m=gallery [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Considering URL example you have provided (http://www.example.com/category/postname/gallery/ => http://www.example.com/category/postname/?m=gallery) you will need this sort of line:
RewriteRule ^(.+)/gallery/$ $1/?m=gallery [QSA]
Added missing slashes /
Added [QSA] flag (may not be really necessary actually).
You would need to put this rule somewhere on the top: after this line, for example: RewriteRule ^index\.php$ - [L]
Obviously, you ensure that WordPress will generate this kind of URLs: http://www.example.com/category/postname/gallery/

Resources