Remove specific slug in with htaccess - apache

I have a Wordpress site web in localhost with XAMPP. Now I have a URL like this:
http://localhost:82/subdirectory/images.html/nggallery/tags/[tagname]
Now I want this:
http:// localhost:82/subdirectory/images/nggallery/[tagname]
To remove tags, I've tried many codes, like this:
RewriteRule ^tags/(.+)/$ /$1 [R=301,L]
but doesn't work.
Help me, please.
I'm using WordPress, my htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>
# END WordPress

Before answering, a better idea than using htaccess for this is to use Wordpress' rewrite modules.
Anyway, you need to put your specific rules before Wordpress' main rule.
You can replace your current code by this one in your htaccess (which has to be in your subdirectory folder)
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /subdirectory/
# redirect /.../tags/XXX to /.../XXX
RewriteCond %{THE_REQUEST} /images\.html/nggallery/tags/([^\s\?]+)\s [NC]
RewriteRule ^ images/nggallery/%1? [R=301,L]
# internally rewrite /.../XXX to /.../tags/XXX
RewriteRule ^images/nggallery/(.+)$ images.html/nggallery/tags/$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
# END WordPress

try
http:// localhost:82/subdirectory/images/nggallery/[tagname]
RewriteRule ^nggallery/tags/(.*)$ http://localhost:82/subdirectory/images/nggallery/$1

Related

Rewrite rule for opening a different page

I'm stuck with some .htaccess rules.
I've got the page /module/slug. I want, when I type slug to see content of /module/slug/ but not redirect to this page. Is it possible? I've tried this rule, but had no luck:
RewriteRule ^slug/$ /module/slug/ [L]
My .htaccess content:
# 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]
RewriteRule ^calculator/$ /sample-page/ [R=301,L]
</IfModule>
# END WordPress
That happens because you don't tell Apache to redirect (R=301).
Add the redirect flag R, and it should work. Also make sure that:
You have set the RewriteBase correctly.
There are not any other rules earlier in the file that match the same URL.
.
RewriteEngine On
RewriteBase /
RewriteRule ^slug/$ /module/slug/ [R=301,L]

Apache .htaccess RewriteRule not including subdirectory when redirecting non-www to www url

I've setup a rewrite rule in order to direct domain.com traffic to www.domain.com using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
From my limited understanding, the $1 in the RewriteRule should match anything after domain.com/ and then be placed at the end of my rewritten url www.domain.com/.
For example domain.com/abc should become www.domain.com/abc.
However this is not working. If I browse to domain.com/abc then this path isn't rewritten at all and I just get domain.com/abc without the www.
I've done lots of reading just to figure out that the $1 should be taking care of this, from my understanding.
Can anyone explain why it isn't working as I suspect it should be? Thanks.
It turns out that because I was using Wordpress AND W3 Cache plugin that I was simply putting the code in the wrong place in my .htaccess file.
The code should be placed within the # BEGIN Wordpress codeblock and before the existing rules that Wordpress already has in place.
The existing code should look something 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
I added my original code to this as follows:
# BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#END Wordpress
This fixes the issue I was describing above. Thanks to the commenters for sparking my thought process.

301 redirect with query parameters isn't working in .htaccess

I have a new website recently uploaded on a domain name and I want to redirect individually all my previous pages to the new ones. (they aren't more than 20)
WHat i've tried to accomplish is the following, but for some reason it isn't working:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=16&page=PRICES_SKI_PACKS$
RewriteRule ^(.*)$ http://www.skibansko.bg/prices-ski-packs/ [L,R=301]
Old url: http://skibansko.bg/index.php?id=16&page=PRICES_SKI_PACKS
New url: http://www.skibansko.bg/prices-ski-packs/
It simply is redirecting me to the home page of the new website.
The old website was not on Wordpress and the new one is on Wordpress
The old and the new websites were on the same domain as you can see from the url's.
Probably the WordPress rewriting rules are causing it, you can check them here:
# 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
And my whole .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
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=16&page=PRICES_SKI_PACKS$
RewriteRule ^(.*)$ http://www.skibansko.bg/prices-ski-packs/ [L,R=301]
Thank you all in advance!

rewrite htaccess from xyz.com/?type=test&s=test2 to xyz.com/test/test2

what i need is for a search thing, so the test2 is changeable and need the link to change too.
how can i do this in .htaccess?
this is what i thought to do.
RewriteRule ^xyz.com/test/(.*)$ ^xyz.com/?type=test&s=$1 [NC,L]
any idea?
Thanks!
my Wordpress .htacess:
# 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]
RewriteRule ^([^/]+)/(.*)$ index.php?type=test&s=$1 [NC,L]
</IfModule>
# END WordPress
You will be looking at something like this:
RewriteRule ^([^/]+)/(.*)$ ?type=$1&s=$2 [NC,L]
Not sure it will work without the filename, though. If that's the case point the rewrite to your file that handles requests, e.g.:
RewriteRule ^([^/]+)/(.*)$ index.php?type=$1&s=$2 [NC,L]
You don't include the domain name in the RewriteRule match pattern.

Redirecting root only via .htaccess with other rules in place

I have an installation of YOURLS that redirects generated addresses to a different domain, however the root does not redirect. How can I set a rule that cooperates with the existing rules to achieve this? Here's the .htaccess file as it stands:
# BEGIN ShortURL
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]+)/?$ yourls-go.php?id=$1 [L]
# END ShortURL
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
</IfModule>
I have a basic understanding of these methods, however I do not know how separate rules affect one another. Thanks in advance for your help.
It will probably suffice if you replace the + quantifier (one or more repetitions) of your first rule with the * quantifier (zero or more repetitions).
Try this:
# BEGIN ShortURL
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]+)/?$ yourls-go.php?id=$1 [L]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^([0-9A-Za-z]*)/?$ yourls-go.php?id=$1 [L]
# END ShortURL
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization},L]
</IfModule>