How to remove id from this url? - .htaccess

I need to remove /photo/26/ from this url http://localhost/photo/26/h-house
I already have removed index.php from all links.

You can do something like the following at the top of your .htaccess file using mod_rewrite to redirect /photo/26/h-house to /h-house, effectively removing /photo/26 from the URL:
RewriteRule ^photo/26/h-house$ /h-house [R=302,L]
Or, to avoid repetition, you can do the following instead:
RewriteRule ^photo/26(/h-house)$ $1 [R=302,L]
$1 is a backreference that contains /h-house from the capturing group in the RewriteRule pattern.

Related

How to write a htaccess rewrite rule where you can load a website inside a folder as it would be loaded from the root

I have the following folder structure
ROOT (loaded on domain.com)
+campaigns
|+campaign1
|-assets
|-index.php
|-campaign2
.htaccess
index.php
stuff.php
At the moment to access the website inside the folder campaign1 i would have to enter in the URL address bar: domain.com/campaigns/campaign1
What should i put in the .htaccess file so that when you put
domain.com/campaign1 the browser loads and shows everything from domain.com/campaigns/campaign1 but of course without visibly changing the URL in the address bar.
Thank you so much for your help.
You could do something like the following in the root .htaccess file using mod_rewrite. This is a general version if the "campaign" subdirectories are not known (or too many):
RewriteEngine On
# 1. Abort early if request already maps to a file or directory
RewriteRule ^campaigns/ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# 2. Check if the first path-segment maps to a directory in "/campaigns` directory
# If so then internally rewrite the request to that subdirectory
RewriteCond %{DOCUMENT_ROOT}/campaigns/$1 -d
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
Where $1 is a backreference to the captured subgroup in the RewriteRule pattern.
i would have to enter in the URL address bar: example.com/campaigns/campaign1
Since campaign1 is a directory you should be requesting campaign1/ (with a trailing slash) otherwise mod_dir is going to issue an external redirect to append the trailing slash.
when you put example.com/campaign1 the browser loads and shows everything from ...
Likewise, you should be requesting example.com/compaign1/ - with a trailing slash - and the above rule assumes that you are. It won't do anything if the trailing slash is omitted and you will get a 404. (If you are expecting third party requests where the trailing slash is omitted then you will need to manually issue a 301 redirect to append the trailing slash before the above rule.)
UPDATE:
What if i know the name of the campaign folder that goes into the campaigns folder? basically if i want to manually add the rule but campaign specific?
Yes, you can do this. In fact, if the number of campaigns you wish to rewrite in this way is limited then this may even be preferable since it avoids the filesystem check.
You may also be able to remove the second rule (second part of section#1 above) that prevents further processing should a file or directory be requested. ie. Remove the following:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
So, instead of (or before) section#2 above you could do the following...
For a single campaign:
# 2. Rewrite campaign to "/campaigns" subdirectory
RewriteRule ^compaign1/ campaigns%{REQUEST_URI} [L]
For multiple campaigns:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 =campaign1 [OR]
RewriteCond $1 =campaign2 [OR]
RewriteCond $1 =campaign3
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
Note that it is specifically $1 =campaign1, the = is a prefix-operator on the CondPattern (2nd argument) itself. The = operator makes it an exact string match, not a regex.
Or, using a regex and alternation:
# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 ^(campaign1|campaign2|campaign3)$
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]
(Although this could be combined into a single directive.)
UPDATE#2:
Add this before the internal rewrites to add a trailing slash at the end of the URL:
RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]
Note that all internal links must already linking to the URL with a trailing slash. This redirect is only for the benefit of search engines and third party links that might be referencing the non-canonical URL without a trailing slash.

How to redirect url via .htaccess

I want to redirect via .htaccess
https://www.example.co/en/brand/Abc to https://www.example.co/en/brand/abc
I have tried
RewriteRule ^https://www.example.co/en/brand/Abc https://www.example.co/en/brand/abc [R=301,L]
The RewriteRule pattern (1st argument to the RewriteRule directive) matches against the path-part of the URL only, ie. /en/brand/Abc. An additional complication in per-directory .htaccess files is that the URL-path that is matched is also less the directory prefix (which always starts with a slash), so the URL-path does not start with a slash. In other words: en/brand/Abc (for an .htaccess file in the document root).
So, you will need to format the directive like this instead:
RewriteRule ^en/brand/Abc$ https://www.example.co/en/brand/abc [R=301,L]
(Assuming you already have RewriteEngine On defined and that this is near the top of your .htaccess file.)
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You may try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Don't want loops
RewriteCond %{REQUEST_URI} !/abc
RewriteCond %{REQUEST_URI} /Abc
RewriteRule . https://www.example.co/en/brand/abc [R=301,L]
URL are usually case-sensitive. Check this document, while domain names are not. Therefore "abc" and "Abc" are not the same and that's what the question is about. I think.

How to escape characters in wildcard in htaccess

I have a htaccess file which is used for earning money by short links, as the following code:
RewriteEngine On
RewriteRule (.*) http://shortlink.com/s/N9IpzM7J?s=$1 [R=301]
But with above code, it'll redirect example.com/https://google.com to http://shortlink.com/s/N9IpzM7J?s=https:/google.com notice it that it lost a slash at https://, in my thought, it might be a special character in htaccess but I don't know how to escape it.
So I want to ask how to let the above code works which will redirect example.com/https://google.com to http://shortlink.com/s/N9IpzM7J?s=https://google.com?
Because of this post I replace by link shortening service's url to shortlink.com!
If you want to capture full URI, you should use RewriteCond directive.
Apache automatically strips off multiples slashes into a single slash in RewriteRule directive.
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^ http://shortlink.com/s/N9IpzM7J?s=%1 [R=301,L]

Using mod rewrite to redirect, using query string value with characters replaced

I have old URLs like this:
http://www.foo.org/cgi-bin/search.cgi?Blog=14&tag=my%20long%20tag%20name&limit=100
which I want to redirect to another server's page like:
http://www.bar.org/tag/my-long-tag-name/
On foo.org I have:
RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*)&.*$ [NC]
RewriteRule ^cgi-bin/search.cgi$ http://www.bar.org/tag/%1/? [NC,R=301,L]
This redirects to the correct page on bar.org, where I need to replace the %20s with hyphens. However, it changes every %20 to %2520. I've tried adding the NE flag, but no change. This is the thing I'm stuck with.
Once there, this rule replaces %20s with hyphens:
RewriteRule ^blog/tag/([^\s]*)(?:\s)+(.*)/$ /blog/tag/$1-$2/ [R=301,L]
(Bonus points... some of those original tag values also have %28 and %29 in them, which ultimately I'd like to delete. So a tag of bob%20and%20%28thelma%29 becomes bob-and-thelma.)
You can use these rules in site root .htaccess of www.foo.org:
RewriteEngine On
# temporary rewrite to /tag/tag-name
RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*) [NC]
RewriteRule ^cgi-bin/search\.cgi$ /tag/%1? [L]
# redirect to http://www.bar.org if there is no special char in URI
RewriteRule ^tag/[^\x20\x28\x29]+$ http://www.bar.org/$0 [NC,L,NE,R=301]
# if special char is in the end then simple remove it
RewriteRule ^(tag)/([^\x20\x28\x29]*)[\x20\x28\x29]+$ $1/$2 [NC,N,DPI]
# otherwise replace special char by a hyphen
RewriteRule ^(tag)/([^\x20\x28\x29]*)[\x20\x28\x29]+(.+)$ $1/$2-$3 [NC,N,DPI]

Url redirection - changing folder, replacing underscores with dashes and removing html extension

I have an old site where I have underscores and an html extension which I want to redirect in the following way
http://example.com/news/this_is_a_test.html -> http://example.com/post/this-is-a-test
http://example.com/portfolio/another_test.html -> http://example.com/project/another-test
There are other folders apart from news and portfolio and clearly the final segment of the url has an unknown number of underscores.
Here is the .htaccess I am using at the moment (based on my original question htaccess file to remove folder, and replace underscores with dashes). It works for the news example, but breaks if I try for portfolio.
Any idea where I'm going wrong?
RewriteEngine on
# redirect "/news_bar" to "/foo_bar"
RewriteRule ^news/(.+)$ /$1 [L,R]
#2 replace underscore with hypens
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /post/$1 [L,R]
RewriteRule ^portfolio/(.+)$ /$1 [L,R]
RewriteRule (.*)_(.*) $1-$2 [N,E=uscores:yes]
RewriteCond %{ENV:uscores} yes
RewriteRule ^(.+)$ /project/$1 [L,R]
# remove .html from end of url
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Many thanks!
I had a solution for this before, but it is breaking on my current setup (Apache crashes), and so it would be unwise of me to recommend it for your case. (It could be an issue with my setup, but I'd prefer to give you a more straight-forward route.)
This solution involves sending the relevant requests to a PHP file that will do the necessary replacements, and redirect once only. Note that your current implementation will send multiple redirect instructions to the browser. This is not only bad from a user experience point of view, but also from an SEO one.
To implement the solution, start by replacing your .htaccess directives with this:
RewriteEngine On
# Rewrite news and portfolio links to redirect.php
RewriteRule ^(news|portfolio)/(.+).html /redirect.php [L]
Then, create a redirect.php file in the same directory as your .htaccess file (in this case, your document root), and fill it with this simple replacement method and redirect instruction:
<?php
$path = $_SERVER['REQUEST_URI'];
# Perform the necessary replacements. The first array contains
# what we're searching for, bit by bit, and the second array
# contains the relevant replacements.
$path = str_replace(
['_', '/news/', '/portfolio/', '.html'],
['-', '/post/', '/project/', ''],
$path);
# Now, simply redirect to the new path.
# Change 302 to 301 use a "Moved Permanently" header,
# resulting in browsers and search engines caching
# the redirect.
header("Location: $path", true, 302);

Resources