Ignore subfolders of URI in .htaccess - regex

I have an app where you can send a link to content. The link is build like this:
domain.com/share/param1/param2/param3
The app is opened if the user has the app installed, otherwise it will open the website. For those who have not the app installed I got a webpage at domain.com/share where links to download the app are placed. The problem is that if you call / share/.../.../... you don't get redirected to /share.
I want to redirect everything with /share/... beginning to /share
I'm running Wordpress on an Apache
What I tried:
RewriteEnginge On
#1
RewriteRule ^share/.*$ share
#2
RewriteRule ^share/(.*)$ share
#3
RewriteRule ^share\/(.*)$ share
#4
RewriteRule share/.* share
#5 this is to exclude an error to Regex
RewriteRule share/test share
The rest of the .htaccess which (hopefully) doesn't have an impact:
# BEGIN Thunermay (Admin)
# set apple-app-site-association as application/json for Apple's crawler
<Files "apple-app-site-association">
Header set Content-type 'application/json'
</Files>
# ensure https
RewriteEngine On
# redirect all /share/anything links to /share
# Here was I trying to get it to work #
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
# END Thunermay
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Thanks in advance!

The problem was, as #Cbroe stated, that I didn't redirect to the page but would only internally redirect, where the problem is, that this site does not exist. The solution was:
RewriteRule /share/.+ share [R,L]
The + is to have at least one character because otherwise it ended in an infinite loop, the R is to redirect it and not only show it in the URI and the L is for that it is the last rule and Wordpress does not catch it and point to the landing page.
The slight problem remaining is that my https redirect does not work anymore IF I have a share link and use http which my URI-generator does not, so it should not be an problem.

Related

Apache rewrite rules for maintenance, htaccess not allowing my IP address through

I'm working on a template to use for site maintenance. Took a while to figure out that Apache wanted absolute paths, but now everything is working, short of the IP.
I'm trying to allow my IP and the IP of the server (domain) when maintenance is activated, so the maintenance page will only be served to guests. However, I'm also getting served the maintenance page. I suck at regex, so it might be a simple error.
Here are my Apache directives.
## Maintenance
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# local ip
RewriteCond %{REMOTE_ADDR} !^111\.111\.33\.44
# server ip
RewriteCond %{REMOTE_ADDR} !^222\.222\.333\.444
# maintenance folder
RewriteCond %{REQUEST_URI} ^/maintenance/
RewriteRule .+ - [L]
# maintenance files
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|woff|woff2|eot|ttf|svg) [NC]
RewriteCond %{REQUEST_URI} !^/maintenance/maintenance\.html$
RewriteRule ^(.*) https://example.com/maintenance/maintenance.html [R=307,L]
</IfModule>
ErrorDocument 503 /maintenance/maintenance.html
ErrorDocument 307 /maintenance/maintenance.html
<IfModule mod_headers.c>
#do not cache
Header Set Cache-Control "max-age=0, no-store"
</IfModule>
## End Maintenance
# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
I managed to figure this out, so in case anyone else who's as bad at regex as I am comes across this, it may help them.
I had two unneeded lines of directives and the ^ was unnecessary or causing problems. $ after IP is to make sure a similar longer IP can't get through. I also switched to a 302 error since it is a temporary redirect for maintenance.
Make sure your folder and html file path is set correctly and uses absolute paths. If you aren't using a folder, the path directly to the html should work. I used a folder because I have images and fonts also loading and like to keep things tidy.
<IfModule mod_rewrite.c>
RewriteEngine On
# local ip
RewriteCond %{REMOTE_HOST} !^111\.111\.22\.33$
# server ip
RewriteCond %{REMOTE_ADDR} !^222\.222\.333\.444$
# maintenance folder
RewriteCond %{REQUEST_URI} !^/maintenance/(.)*$
# maintenance files
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|woff|woff2|eot|ttf|svg) [NC]
RewriteCond %{REQUEST_URI} !/maintenance/maintenance\.html$ [NC]
RewriteRule .* /maintenance/maintenance.html [R=302,L]
</IfModule>
<IfModule mod_headers.c>
#do not cache
Header Set Cache-Control "max-age=0, no-store"
</IfModule>

Wordpress: htaccess redirect (301) doesnt work

I have a task which drives me nuts: I have an old blog and want to create a 301 redirect to my new blog for several posts because I moved some of the content to the new domain.
Let's say one of my old posts is to be find at example.com/this-is-my-post and visitors should be redirected to example.org/my-old-post-2817.
What I tried is this:
# BEGIN Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post$ http://example.org/my-old-post-2817 [R=301]
</IfModule>
# END Redirect
The problem is that there is no redirect in FF (desktop), Chrome (Android) and standard internet browser (Android). In IE (desktop) I get an error stating that the page can't be displayed. No reason is given.
I made sure not to have any unneccessary blank lines in the .htaccess and, to be on the safe side, deleted my browser's histories, caches and so on.
A CCA of my provider dropped me a message that he re-saved the mod rewrite module and that I have to try it 15 minutes later again. With no success.
So, what did I wrong?
Regards,
Crunchy
Change order of rules to keep R=301 rules before internal WP rules:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post/?$ http://example.org/my-old-post-2817 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

htaccess redirect /foldername/page_name to /page-name

I have an issue:
Recently I replaced my old website (no CMS) with a new website (and WordPress as CMS). But a lot of people still try to access the website by old URL's.
Obviously I want to redirect them to the right page.
For example: the previous URL was: http://domain.com/foldername/page_name
On the new website that same page is located at http://domain.com/page-name
Note #1: The underscore has been replaced with a dash
Note #2: Some pages may include more underscores. For example: http://domain.com/foldername/an_other_page_name
Note #3: Since I use WordPress as CMS, there is already some code in my .htaccess file.
# 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
Since I'm not as good with htaccess as some of you I hope someone has the solution, since I'm losing visitors because they can't find the right page.
Just below RewriteBase line you can use this recursive rule:
RewriteRule "^(foldername)/([^_]*)_+([^_]*_.*)$" /$1/$2-$3 [N]
RewriteRule "^(foldername)/([^_]*)_([^_]*)$" /$2-$3 [L,R=301]

.htaccess redirect old html page with space in the name

I've changed an html site to a WordPress installation.
The old .html links have been redirected to the right wordpress page. The old site, which was obviously build by a monkey, had a page /over ons.html. That's right, it had a space in the filename. So now, i'am getting external requests on /over%20ons.html. I can't figure out how i can solve this redirection problem in .htaccess. The page it has to refer to is /over-ons/.
This is my .htaccess so far:
RedirectMatch 301 ^/([^/.]+)\.html$ /$1/
Redirect 301 /over%20ons.html http://www.sterkermerk.nl/over-ons/
Redirect 301 /index.html http://www.sterkermerk.nl/
# 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
The 2nd line contains the rule. I've tried to escape the %20, tried using regex in RedirectMatch, but I can't get it to work.
Anyone got an idea?
Use mod_rewrite instead of mod_alias (Redirect statements) because you're already using mod_rewrite and they'll conflict with each other:
RewriteRule ^over\sons\.html$ http://www.sterkermerk.nl/over-ons/ [L,R=301]
RewriteRule ^index\.html$ http://www.sterkermerk.nl/ [L,R=301]
RewriteRule ^([^/.]+)\.html$ /$1/ [L,R=301]
(replacing the Redirect* statements)
The URI is decoded before being sent through rewrite rules so you don't need to use the %20, just \s or \ will match the space.

301 redirection help on wordpress

I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.
First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.
In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html