RewriteRule for blog posts with a subdomain in the URL - php

working on a writing a redirect rule but I'm not too familiar with them or with the syntax.
Basically I have Wordpress URLs that look like this:
https://example.com/blog/blog-title/garbagetext
I need to be able to redirect URLs like this to our 404 page, and my first attempt was with:
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
This worked, but it also made it impossible to access actual blog posts, and is also redirecting URLs that look like this:
example.com/blog/blog-title
Any ideas how to make this work properly?

RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
:
example.com/blog/blog-title
The directive you posted could not redirect the example URL that you say is being erroneously redirected. So, either you have other directives (possibly in other .htaccess files, or the server config) or there is a conflict with existing directives, or you are seeing a cached response? If you've previously experimented with 301 redirects then these will have been cached aggressively by the browser. (It can be easier to test with 302 - temporary - redirects for this reason.)
Since you are using WordPress (which uses a front controller) then any redirects should appear at the start of the file, before the front controller.
You also appear to have an erroneous slash at the end of the RewriteRule pattern that will fail to match an intended URL of the form /blog/blog-title/garbagetext.
I'll ignore the backticks for now - I assume they are probably just a styling typo in your question?
I need to be able to redirect URLs like this to our 404 page
You shouldn't be redirecting to a 404 page. It should be served with an internal subrequest instead. If you redirect then you first send a 3xx response back to the client, the client then issues a second request for your 404.php page (exposing the URL) and then you have to make sure you are manually setting the appropriate 404 HTTP response header.
So, you need to define the appropriate ErrorDocument and R=404 the response.
The posts that need to be redirected to the 404 are any posts that have text following that trailing slash, so far example: example.com/blog/customer-reviews/asdfa.
Try the following instead, at the top of your .htaccess file:
ErrorDocument 404 /404.php
RewriteRule ^blog/.+/. - [R=404]
This will match /blog/customer-reviews/asdfa, but it won't match /blog/customer-reviews/ (or /blog/customer-reviews). The R=404 flag triggers an internal request to the defined error document. The L flag is not required when using a none 3xx code. When there is no substitution text (ie. no target URL) then specify a hyphen (-) instead.
And make sure you've cleared your browser cache.

Related

Magento htaccess redirection issue

I was trying to redirect a particular URL and all its child URL's to a particular URL. Using 301 redirection this is working fine but the browser URL is showing the test contents also. Please see below for more details
Example: https://www.mywebsite.com/customer/account/login/referer/* needs to be redirected towards https://www.mywebsite.com/customer/account/login/register
What I tried is:
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /customer/account/login/referer https://mywebsite.com/customer/account/login/register
The htaccess rule is working fine for just this URL. If I type
https://mywebsite.com/customer/account/login/referer/testing
This will be redirected towards https://mywebsite.com/customer/account/login/register/test
Please let me know how I will be able to trim the other part (URL part after register/*)from the redirected URL. What I am trying to achieve is
https://mywebsite.com/customer/account/login/referer/* [ANY URL's AFTER referer/ (Including /referer) needs to be redirected to https://mywebsite.com/customer/account/login/register].
This is expected behaviour, as stated in the documentation of Redirect
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
To redirect all requests starting with this prefix to the given URL, you may use RedirectMatch, which uses regular expressions instead of a URL prefix
RedirectMatch ^/customer/account/login/referer https://mywebsite.com/customer/account/login/register
You don't need to use
Options +FollowSymLinks
RewriteEngine on
because this is for mod_rewrite. Redirect and RedirectMatch are provided by the mod_alias module.

Display the 404 page in a multi-language website

I created a 404.php page for a website. Also, there is the .htaccess file ( in the /root ) having the ErrorDocument 404 /404.php line.
The website is having a multi-language functionality, something like this:
sitename.com/it/article1
sitename.com/en/article1
and so on ... There are numerous articles.
The 404.php page appears when I'm trying to access something like sitename.com/adsdasaerera but it doesn't appear when I'm trying to access sitename.com/en/adsdasaerera, adsdasaerera not being, obviously, an existing article.
How can I achieve this?
Ideally you would have one error 404 document page. On that page you would first set a default locale of en, and then determine the users browser locale/language they are using, and proceed to the next step which is displaying the error page in that language.
Since we are talking PHP, here is an answer how to detect the locale in PHP.
Simplest way to detect client locale in PHP
From there, the next step is basically using the strings associated for that locale from an array or a file and displaying them.
On this page you could also have some links to other translations that could be viewed(hidden elements that show div on click), if the user doesn't wish to read the error in the language their browser is set to use.
Did you try giving it a full path?
ex ErrorDocument 404 /site/error/404.html
Have you tried put the line ErrorDocument 404 /404.php into the apache global config file, maybe in /etc/httpd/.
Or is there an exising .htaccess file under somesite.com/en/ folder?
how do you archieve the multi-language functionality? rewrite in apache or .htaccess? and what does your PHP code?
in addition to Branimir Đureks answer, you may analyze the $_SERVER['REQUEST_URI'] string i.e. using explode() and if the article dosn't exists in the choosen language, send header("HTTP/1.0 404 Not Found"); otherwise display the article.
If you are NOT using a CMS etc, so the Language Folders are really existing, you can use this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(ie|en|de)/ /404.php/$1 [R=404,L]
Explanation
The three conditions check that the requested file or folder does not exist
The rule checks that the requested url starts with one of the three countries then a /, captureing the country code to Group 1
It redirects to /404.php?lang=$1, e.g. /404.php?lang=en with a 404 code
In that 404.php you just need to Use $_GET['lang'] to get the requested Langugage.
But you already said, that the multi-language functionality is archieved in .htaccess. It would be helpfull to know whats in that file.
Anyway: If all Requests get redirected to one page (e.g. index.php), somethere in that file the Content of the site gets included. That hapens either with an include or similar of a file or with a Query to the Database.
Thats the point there you need to expand your code. If that file isn't found or if there is no record in the Database, you need to include your 404 File.
Please use this code at the bottom of .htaccess. Replace Path with your path.
RewriteRule ^pagenotfound$ 404.html
ErrorDocument 404 path/pagenotfound
It doesn't redirect you to the 404.php file because you accessed the existing file. Look at this URL
sitename.com/en/adsdasaerera
"adsdasaerera" is probably only a parameter value that is rewrited by htaccess. It's not a file. I suppose you have a "en" folder and index.php in it. So when you enter url above, you access that index.php in "en" folder with parameter "adsdasaerera" and that's the reason why you don't get 404 error. You can solve it by adding little code that searches trough the database for "adsdasaerera" and if it doesn't exist, in your code manually redirect it to 404.php file.
The reason why you get 404.php on this url
sitename.com/adsdasaerera
is because you don't have file named "adsdasaerera".
Hope it will help you :)
It should be possible to define an ErrorDocument like this in htaccess file:
ErrorDocument 404 http://sitename.com/404.php
and then do a language specific redirect check for requests in 404.php file.

php htaccess redirect 404 error page

I am building a LAMP application www.testing.com and I have an image url www.testing.com/image/folderA/a.jpg.
I want to have a rewrite rule in .htacess such that if user navigates to images that do not exist in /image/foldera, he will be redirected to another image with the url www.testing.com/image/folderB/b.jpg.
This is my code.
RewriteRule ^image/folderA/(.*\.(jpg|gif|png))$ http://%{SERVER_NAME}/image/folderB/b.jpg [R=404,L]
However, this is not working as www.testing.com/image/folderA/a.jpg, which is supposed to return an image, gives an 404. Any idea why?
When using the R flag you need to keep the following in mind:
However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.
http://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_r
This means that your rewrite rule is dropping the substition string and redirecting all requests to specified files to the built-in 404 page.

Htaccess wrongly making 301 redirect

I'm just trying to display css content at http://mydomain.com/dynamic_css/presets/
when user's browser loads http://mydomain.com/css/dynamic.css using the following
RewriteEngine on
RewriteRule ^dynamic\.css$ http://mydomain.com/dynamic_css/presets/ [QSA,L]
But instead browser gets http 301 redirected to http://mydomain.com/dynamic_css/presets/
Any idea why ?
Basically, the script at http://mydomain.com/dynamic_css/presets/ shows up a CSS generated code to allow more styles control from admin settings.
You can't specify the full domain in your request. mod_rewrite assumes this is an external URL which can only be handled via redirect. Try the below instead.
RewriteRule ^dynamic\.css$ /dynamic_css/presets/ [QSA,L]

htaccess redirect url with var from no file extension to file extension

the problem is that for some reason googlebot gets a bunch of error 404s when crawling a certain page but everything works fine on my end, I think...
using htaccess, I have rewritten a page with vars for seo purposes, so:
RewriteRule ^feeds/([^/]*)\.xml/?$ /rss/feeds.php?cat=$1 [QSA,NC,L]
so I get www.url.com/feeds/category.xml
instead of
www.url.com/feeds.php?cat=category
I've also ajusted all urls on the page to follow this rule and like I said everything works fine on my end... googlebot for some reason only sees www.url.com/feeds/category and does see the .xml extention.
not sure if it would work but I was hoping to put a rule in htaccess to redirect www.url.com/feeds/category to www.url.com/feeds/category.xml in the even that this happened, but was unsure of how to do so.
Maybe try
RewriteRule ^feeds/([^/]+)/?$ /feeds/$1.xml [R,NC]
Which would redirect www.url.com/feeds/category to www.url.com/feeds/category.xml, using a HTTP 302 Redirect. Remove the R flag if you don't want it do use the HTTP 302 Redirect, but that would make www.url.com/feeds/category a valid URL as far as google is concerned. Just in case: mod_rewrite.

Categories